File download using WebResponse

R

Rick

The following code download the file but it does not bring the file Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save dialog
box?


public static int DownloadFile()
{
int bytesProcessed = 0;

Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;

string remoteFilename = "http://MyServer/StoredFiles/MyFile.xls";
localFilename = @"c:\files\dMyFile.xls";

try
{
WebRequest request = WebRequest.Create(remoteFilename);

if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();

localStream = File.Create(localFilename);

byte[] buffer = new byte[1024];
int bytesRead;

response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("Content-Disposition",
"attachment; filename=" + localFilename)

do
{
bytesRead = remoteStream.Read(buffer, 0,
buffer.Length);
localStream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}

return bytesProcessed;
}
 
F

Family Tree Mike

Rick said:
The following code download the file but it does not bring the file
Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save
dialog
box?


public static int DownloadFile()
{
int bytesProcessed = 0;

Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;

string remoteFilename = "http://MyServer/StoredFiles/MyFile.xls";
localFilename = @"c:\files\dMyFile.xls";

try
{
WebRequest request = WebRequest.Create(remoteFilename);

if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();

localStream = File.Create(localFilename);

byte[] buffer = new byte[1024];
int bytesRead;

response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("Content-Disposition",
"attachment; filename=" + localFilename)

do
{
bytesRead = remoteStream.Read(buffer, 0,
buffer.Length);
localStream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}

return bytesProcessed;
}


Your code doesn't even declare an open save dialog :)

Instead of hardwiring the filename localFilename, use the following:

SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() != DialogResult.OK) return -1;
localFilename = sfd.FileName;

Is there a reason you aren't using WebClient.DownloadFile()?
 
R

Registered User

The following code download the file but it does not bring the file Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save dialog
box?
- snip -

The code below receives a file from a remote server via an
HttpWebRequest. The response's stream is written to memory rather than
disk. The memory stream is then written to the page's Response
object's stream.

//
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(...);
// assign request properties
....
// response from remote server
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// read the response stream into a byte buffer
MemoryStream ms = new MemoryStream();
const int BufLength = 1000;
byte[] b = new byte[BufLength];
int c = 0;
do
{
c = response.GetResponseStream().Read(b, 0, BufLength);
ms.Write(b, 0, c);
}
while (c > 0);
// write stream to Response
ms.WriteTo(Response.OutputStream);
Response.Flush();

regards
A.G.
 
R

Rick

My understanding is that the line response.ContentType =
"application/vnd.ms-excel";
should opne the save dialog box.
Family Tree Mike said:
Rick said:
The following code download the file but it does not bring the file
Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save
dialog
box?


public static int DownloadFile()
{
int bytesProcessed = 0;

Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;

string remoteFilename = "http://MyServer/StoredFiles/MyFile.xls";
localFilename = @"c:\files\dMyFile.xls";

try
{
WebRequest request = WebRequest.Create(remoteFilename);

if (request != null)
{
response = request.GetResponse();
if (response != null)
{
remoteStream = response.GetResponseStream();

localStream = File.Create(localFilename);

byte[] buffer = new byte[1024];
int bytesRead;

response.ContentType = "application/vnd.ms-excel";
response.Headers.Add("Content-Disposition",
"attachment; filename=" + localFilename)

do
{
bytesRead = remoteStream.Read(buffer, 0,
buffer.Length);
localStream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}

return bytesProcessed;
}


Your code doesn't even declare an open save dialog :)

Instead of hardwiring the filename localFilename, use the following:

SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() != DialogResult.OK) return -1;
localFilename = sfd.FileName;

Is there a reason you aren't using WebClient.DownloadFile()?
 
F

Family Tree Mike

Rick said:
My understanding is that the line response.ContentType =
"application/vnd.ms-excel";
should opne the save dialog box.
:


That is true when you are coding the server side code. You are on the
client in what you have shown.
 
R

Registered User

My bad, I was thinking file dialog on an ASP.NET client. Use a
FileSaveDialog to determine if the file should be saved and to where.
- snip -

The code below receives a file from a remote server via an
HttpWebRequest. The response's stream is written to memory rather than
disk. The memory stream is then written to the page's Response
object's stream.

//
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(...);
// assign request properties
...
// response from remote server
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// read the response stream into a byte buffer
MemoryStream ms = new MemoryStream();
const int BufLength = 1000;
byte[] b = new byte[BufLength];
int c = 0;
do
{
c = response.GetResponseStream().Read(b, 0, BufLength);
ms.Write(b, 0, c);
}
while (c > 0);
// write stream to Response
ms.WriteTo(Response.OutputStream);
Response.Flush();

regards
A.G.
 
R

Rick

The code I posted is a server side code and will be residing on the server.
The code is written in C#.net to generate a dll which is used in an a web
based application.
 
F

Family Tree Mike

Peter Duniho said:
I don't know what the above is supposed to mean. It seems that you are
possibly conflating the question of the "server" in the sense of where the
hosting of your application is done, with the question of the "server" in
the sense of the specific connection via which the data is being
transferred.

The latter is what's important here. And in the code you posted, it seems
clear to me that the code post is the "client" (i.e. it's the end that
initiates contact and receives the data), not the "server". But so far
there's not enough specifics in your question to know for sure.

In any case, a C# application is not by itself going to automatically
display any sort of file dialog. A web browser client might do so when
downloading a file, but when it does so it is doing basically the same
thing a C# application would have to do: note that the download is a file
type, and explicitly present the dialog to the user by calling the
appropriate platform API to show the dialog.

Pete

If this is serverside, then you would have no knowledge of the folder
"c:\files". That doesn't make sense to me. If this were serverside code,
you should just need to do something like:

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;
filename=dMyFile.xls");
Response.ContentType = "application/exe";
Response.Write(they bytes that makeup dMyFile.xls);
Response.End

Then on the client the filename will need to be chosen via a dialog shown by
the browser, not by your code.

Mike
 
F

Family Tree Mike

Peter Duniho said:
I don't know what the above is supposed to mean. It seems that you are
possibly conflating the question of the "server" in the sense of where the
hosting of your application is done, with the question of the "server" in
the sense of the specific connection via which the data is being
transferred.

The latter is what's important here. And in the code you posted, it seems
clear to me that the code post is the "client" (i.e. it's the end that
initiates contact and receives the data), not the "server". But so far
there's not enough specifics in your question to know for sure.

In any case, a C# application is not by itself going to automatically
display any sort of file dialog. A web browser client might do so when
downloading a file, but when it does so it is doing basically the same
thing a C# application would have to do: note that the download is a file
type, and explicitly present the dialog to the user by calling the
appropriate platform API to show the dialog.

Pete

Sorry, but my post moments ago should have been parallel to yours, and not a
reply necessarily to your post.

Mike
 
J

Jeff Johnson

The following code download the file but it does not bring the file
Open/Save
dialog box. How do I fix the code so it will bring up the Open/Save
dialog
box?

Are you related to "Ryan"? Because you started what appears to be an
identical thread to "View MS Excel file using HttpWebRequest" from June 3rd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top