Enabling a Client to Download a File

G

Guest

I am wanting to allow a client to download a file from a web server and save it on their local PC. The only way that I have found to do this is to use HTTP headers. I am using the code shown below (which I have copied with some variable name changes) from a book on ASP.NET programming. This code sits in the Page_Load event of a file called 'Download.aspx'. In order to send a file to the client, this page is called from another page (using Response.Redirect), passing it the full location of the file to be sent to the client. Everything works correctly except that the default file name that appears in the SaveAs dialog box on the client is always 'Download.aspx" rather than the name of the actual file. This happens despite the use of the "Content-Disposition attachment filename=..." header, which, according to the comment should set the default file name

Can anyone see what the problem is or suggest an alternative way of doing this? (I am using IE 6 as a browser.

Thanks

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
'Put user code to initialize the page her

Dim sFilePath As Strin
Dim oFile As IO.FileInf

sFilePath = Request.QueryString.Item("FilePath"
oFile = New System.IO.FileInfo(sFilePath

' Clear the current output content from the buffe
Response.Clear(

' Specify that the response is a stream that cannot be read by the client and must be downloade
Response.ContentType = "application/octet-stream

' Add the header that specifies the default filename for the Download/SaveAs dialo
Response.AddHeader("Content-Disposition", "attachment filename=" & oFile.Name

' Add the header that specifies the file size, so that the browser can show the download progres
Response.AddHeader("Content-Length", oFile.Length.ToString()

' Send the file stream to the clien
Response.WriteFile(oFile.FullName

' Stop the execution of this pag
Response.End(

End Su
 
M

Martin Marinov

It must be somthing with the strings in the header
actually i've worked with downloading files and this code
Response.ContentType = "application/octet-stream; name=" & oFile.Name
Response.AddHeader("content-disposition", "attachment;filename=" &
oFile.Name);

works perfectly

Regards
Martin Marinov

Sleepy said:
I am wanting to allow a client to download a file from a web server and
save it on their local PC. The only way that I have found to do this is to
use HTTP headers. I am using the code shown below (which I have copied with
some variable name changes) from a book on ASP.NET programming. This code
sits in the Page_Load event of a file called 'Download.aspx'. In order to
send a file to the client, this page is called from another page (using
Response.Redirect), passing it the full location of the file to be sent to
the client. Everything works correctly except that the default file name
that appears in the SaveAs dialog box on the client is always
'Download.aspx" rather than the name of the actual file. This happens
despite the use of the "Content-Disposition attachment filename=..." header,
which, according to the comment should set the default file name.
Can anyone see what the problem is or suggest an alternative way of doing
this? (I am using IE 6 as a browser.)
Thanks.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
 
G

Guest

Martin

When I changed the header strings to your versions it worked perfectly. Thanks heaps for your help - this has been driving me nuts

Sleepy
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top