By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Downloading file from remote server with 'Save As' dialogue

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

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

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"kevin" <ke********@hotmail.com> wrote in message
news:8c**************************@posting.google.c om...
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


Nov 16 '05 #2

P: n/a
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

Nov 16 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

<ke********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 16 '05 #4

P: n/a

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
Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

<ke********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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


Nov 16 '05 #5

P: n/a
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
<ke********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

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
Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

<ke********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
> 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
>

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.