473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1927
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: js | last post by:
Hi, how to do this: the page is to be able to allow a user to enter a password so as to download documents. It does not meant for any users to download. It is for some specific users. What I want...
0
by: Ilia Makarenko | last post by:
Hi everyone! I've got a problem with IIS 6.0 when downloading files with size > 4 Mb. So I am trying to download file in chunks, but it doesn't work. Only pert of the file is downloaded (~6.5 of...
4
by: mphanke | last post by:
Hi, how can I connect to a restricted directory on a server and download a file, if there is a newer version, than the one on the local file system? Something like ...
1
by: user2008 | last post by:
Hi all, I want to track how many times visitor download file from my website, for example, when visitor click on a download link, a ASPX page will be requested, after that it will redirect to a...
0
by: bonita | last post by:
If I add the code for user to download the file (e.g. if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the following timeout error: Timeout expired. The timeout period elapsed prior to...
0
by: Sid | last post by:
i am inplementing an intranet using asp 3.0 with vbscript. i want to know how to make my webserver download a file on the client.the file is grater than 60 mb. i have a script which does not...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
8
by: BiT | last post by:
Hello, I'm working right now on project in vb.net 2005 for my company, i need the project to download file from the company web site. In order to get the file i have to give the site address...
3
by: cypherkro | last post by:
Hi I have a requirement to automatically download a file using a WebBrowser control without having the "Download File" pop up windows display. I cannot use Webclient!! I am using VS2005, .net2...
5
by: vinodkus | last post by:
dear sir/madam I have to write a code for download file using asp. please help me thanks in advance
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.