469,883 Members | 1,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

Enabling a Client to Download a File

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

Nov 18 '05 #1
2 1762
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" <an*******@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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 'Put user code to initialize the page here

Dim sFilePath As String
Dim oFile As IO.FileInfo

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

' Clear the current output content from the buffer
Response.Clear()

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

' Add the header that specifies the default filename for the Download/SaveAs dialog Response.AddHeader("Content-Disposition", "attachment filename=" & oFile.Name)
' Add the header that specifies the file size, so that the browser can show the download progress Response.AddHeader("Content-Length", oFile.Length.ToString())

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

' Stop the execution of this page
Response.End()

End Sub

Nov 18 '05 #2
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
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by js | last post: by
reply views Thread by Ilia Makarenko | last post: by
4 posts views Thread by mphanke | last post: by
1 post views Thread by user2008 | last post: by
5 posts views Thread by vinodkus | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.