Connecting Tech Pros Worldwide Help | Site Map

Downloading file from remote server with 'Save As' dialogue

kevin
Guest
 
Posts: n/a
#1: Nov 16 '05
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
Dmitriy Lapshin [C# / .NET MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Downloading file from remote server with 'Save As' dialogue


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" <kennedyk78@hotmail.com> wrote in message
news:8c5bf983.0412090336.6394a719@posting.google.c om...[color=blue]
> 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[/color]

kennedyk78@hotmail.com
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Downloading file from remote server with 'Save As' dialogue


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

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Downloading file from remote server with 'Save As' dialogue


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]
- mvp@spam.guard.caspershouse.com



<kennedyk78@hotmail.com> wrote in message
news:1102597350.178561.162940@z14g2000cwz.googlegr oups.com...[color=blue]
> 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
>[/color]


kennedyk78@hotmail.com
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Downloading file from remote server with 'Save As' dialogue



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:[color=blue]
> Kevin,
>
> You might want to try adding the content disposition header to[/color]
the[color=blue]
> 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[/color]
to save[color=blue]
> as.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
>
>
> <kennedyk78@hotmail.com> wrote in message
> news:1102597350.178561.162940@z14g2000cwz.googlegr oups.com...[color=green]
> > I'm afriad changing the ContentType to "application/octet-stream"
> > results in the same behaviour where the media is downloaded before[/color][/color]
the[color=blue][color=green]
> > 'Save As' dialogue opens.
> >
> > WebClient wc = new WebClient();
> > byte[] byteData;
> > Response.BinaryWrite(byteData
> >[/color][/color]
=wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));[color=blue][color=green]
> > 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[/color][/color]
seams to[color=blue][color=green]
> > take physical paths and not URIs.
> >
> > Regards,
> > Kevin
> >[/color][/color]

Shariq Khan
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Downloading file from remote server with 'Save As' dialogue


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


<kennedyk78@hotmail.com> wrote in message
news:1102604988.830436.93060@z14g2000cwz.googlegro ups.com...[color=blue]
>
> 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:[color=green]
>> Kevin,
>>
>> You might want to try adding the content disposition header to[/color]
> the[color=green]
>> 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[/color]
> to save[color=green]
>> as.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mvp@spam.guard.caspershouse.com
>>
>>
>>
>> <kennedyk78@hotmail.com> wrote in message
>> news:1102597350.178561.162940@z14g2000cwz.googlegr oups.com...[color=darkred]
>> > I'm afriad changing the ContentType to "application/octet-stream"
>> > results in the same behaviour where the media is downloaded before[/color][/color]
> the[color=green][color=darkred]
>> > 'Save As' dialogue opens.
>> >
>> > WebClient wc = new WebClient();
>> > byte[] byteData;
>> > Response.BinaryWrite(byteData
>> >[/color][/color]
> =wc.DownloadData(@"http://192.168.16.133/media/mySummerHoliday.wmv"));[color=green][color=darkred]
>> > 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[/color][/color]
> seams to[color=green][color=darkred]
>> > take physical paths and not URIs.
>> >
>> > Regards,
>> > Kevin
>> >[/color][/color]
>[/color]


Closed Thread