Downloading file from remote server with 'Save As' dialogue

K

kevin

Hi,

Any help with this would be really appreciated!

I'm trying to download a file from a remote server. The access
permissions is okay but the problem I'm facing is that the file is
getting downloaded before the Save As dialogue appears. As we will be
downloading some large files, it's unpractical to have this.

So far the code I have to download the file is

WebClient wc = new WebClient();
byte[] byteData;
Response.BinaryWrite(byteData =
wc.DownloadData(@"http://<remote-server>/media/mySummerHoliday.wmv"));
Response.ContentType="application/wmv";
Response.AddHeader( "content-disposition","attachment;
filename=encryptedindy.wmv");

But as I said this downloads the media before the 'Save As' dialogue.

I also have some code that will save the media from my local drive

FileStream sourceFile = new
FileStream(@"C:\media\mySummerHoliday.wmv", FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ContentType="wmv";
Response.AddHeader( "content-disposition","attachment;
filename=mySummerHoliday.wmv");
Response.BinaryWrite(getContent);

But, I've not been able to modify this to download from a remote
server.

I'm sure I'm missing a trick here somewhere. Any ideas?

Regards,
Kevin
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

Try changing the ContentType to "application/octet-stream".
 
K

kennedyk78

I'm afriad changing the ContentType to "application/octet-stream"
results in the same behaviour where the media is downloaded before the
'Save As' dialogue opens.

WebClient wc = new WebClient();
byte[] byteData;
Response.BinaryWrite(byteData
=wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));
Response.ContentType="application/octet-stream";
Response.AddHeader(
"content-disposition","attachment;filename=mySummerHoliday.wmv");

I think I need to read the data into FileStream, but that only seams to
take physical paths and not URIs.

Regards,
Kevin
 
N

Nicholas Paldino [.NET/C# MVP]

Kevin,

You might want to try adding the content disposition header to the
response stream, like so:

// Add the content disposition header to the response.
Response.AddHeader("content-disposition", "attachment; filename=" +
pstrFileName);

Where pstrFileName is the name of the file you want to recommend to save
as.

Hope this helps.
 
K

kennedyk78

Hi Nicholas,

Cheers, but I already have the content disposition header in. In this
case I just made called the file mySummerHoliday.wmv.

WebClient wc = new WebClient();
byte[] byteData;
Response.ContentType="application/octet-stream";
Response.AddHeader(
"contentdisposition",
"attachment;filename=mySummerHoliday.wmv");
Response.BinaryWrite(byteData
= wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));

What I'm trying to achieve is that it streams in the 'Save As' dialogue
but with my current code it get's downloaded to the page before the
'Save As' dialogue.

Then the 'Save As' dialogue appears and it says it's saved a 4MB file
in 1 second.

I only want the content to be downloaded once you click 'Save' as in
all web based downloads.

I know this should be pretty simple I just can't work it out!

Regards,
Kevin

Kevin,

You might want to try adding the content disposition header to the
response stream, like so:

// Add the content disposition header to the response.
Response.AddHeader("content-disposition", "attachment; filename=" +
pstrFileName);

Where pstrFileName is the name of the file you want to recommend to save
as.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



I'm afriad changing the ContentType to "application/octet-stream"
results in the same behaviour where the media is downloaded before the
'Save As' dialogue opens.

WebClient wc = new WebClient();
byte[] byteData;
Response.BinaryWrite(byteData
=wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));
Response.ContentType="application/octet-stream";
Response.AddHeader(
"content-disposition","attachment;filename=mySummerHoliday.wmv");

I think I need to read the data into FileStream, but that only seams to
take physical paths and not URIs.

Regards,
Kevin
 
S

Shariq Khan

Kevin:

I think that the problem is that you are writing all the data at once in the
response stream and while the user is taking their time to click on the
"Save As" button, browser is downloading the data in the background.
Remember that IE downloads the files to a temp location and then moves it if
the user chooses to save it to a different location. With or without "Save
As" browser will always download the file to the local cache.

May be you can download the file from the remote server (say 192.168.16.133)
into a temp file on your local server and then redirect the response to this
temp file. This is better for performance anyway because then you are
letting the web server do what it does best -- i.e., serve static files.

Cheers,
Shariq Khan


Hi Nicholas,

Cheers, but I already have the content disposition header in. In this
case I just made called the file mySummerHoliday.wmv.

WebClient wc = new WebClient();
byte[] byteData;
Response.ContentType="application/octet-stream";
Response.AddHeader(
"contentdisposition",
"attachment;filename=mySummerHoliday.wmv");
Response.BinaryWrite(byteData
= wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));

What I'm trying to achieve is that it streams in the 'Save As' dialogue
but with my current code it get's downloaded to the page before the
'Save As' dialogue.

Then the 'Save As' dialogue appears and it says it's saved a 4MB file
in 1 second.

I only want the content to be downloaded once you click 'Save' as in
all web based downloads.

I know this should be pretty simple I just can't work it out!

Regards,
Kevin

Kevin,

You might want to try adding the content disposition header to the
response stream, like so:

// Add the content disposition header to the response.
Response.AddHeader("content-disposition", "attachment; filename=" +
pstrFileName);

Where pstrFileName is the name of the file you want to recommend to save
as.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



I'm afriad changing the ContentType to "application/octet-stream"
results in the same behaviour where the media is downloaded before the
'Save As' dialogue opens.

WebClient wc = new WebClient();
byte[] byteData;
Response.BinaryWrite(byteData
=wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));
Response.ContentType="application/octet-stream";
Response.AddHeader(
"content-disposition","attachment;filename=mySummerHoliday.wmv");

I think I need to read the data into FileStream, but that only seams to
take physical paths and not URIs.

Regards,
Kevin
 

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