Library code snippets
Force 'file download' for known file types
Multipurpose Internet Mail Extensions (MIME) is a wonderful thing,
allowing your Web browser to automatically display content of many
different formats natively within the browser window. However,
sometimes, you may want users to download this content to their
client computer rather than simply viewing it in the browser. But
how do you override the browser's determination to render known
MIME types itself? The answer is to use a content disposition
header in your Web page.
Suppose you've written an ASP page that contains a link to a known
MIME type, but you want the user to download the file instead of
viewing it. Add the following to your script:
response.addHeader "content-disposition", "attachment;
Then substitute the actual filename and extension, and it's as good
filename=filename.ext"
as done. When your users click on the link, they'll immediately see
the download dialog box instead of the file's contents.
Related articles
Related discussion
-
Header and Footer in Web page print
by fhajaj (4 replies)
-
help me to get simple requirement
by Slicksim (1 replies)
-
Gridview -> Template Field -> Button
by antti.simonen (1 replies)
-
Classic ASP : Page expires
by chezhian_in05 (0 replies)
-
ASP VS PHP
by paulfp (9 replies)
Related podcasts
-
ASP.NET Caching and Performance
Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
Technical Support Engineer EMEA
in Reading (£50K-£50K per annum) -
Solutions Engineer
in Reading (£50K-£60K per annum)
Does this also work to write a file from memory data to a client machine?
Will the same code work in netscape/mozilla
Hi,
I am simply trying to create the File Download Box when a user clicks on file links. Can you help? Is the code you listed all that is neeeded? I have tried to search for a reliable way on the net and found nothing. Is your code working properly?
Thank you in advance and hope you can help!
Nima
I want to open an xml file in asp.net.
I have used the following lines in my code :
Response.ContentType = "application/vnd.ms-xml";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
My problem is that the xml file is opening in the parent window.
I want to open the xml file in a new browser.
Kindly help me with any solution.
I am trying to force a download to a HTA page in a HTML page, but how do I do this, that script given in a later posting doesn't work, which I figured since I am not creating an .asp page?
Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.
Thanks
Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.
Thanks
Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.
Thanks
Add :
Call Response.Flush()
Call Response.End()
This code works for ASP 3.0, so for IIS 5.0, not only for .aspx pages.
I was able to download the file using the above code, but in the downloaded the page.aspx html content is also getting added in the downloaded file.
I'm using the below code in Page.aspx
Response.AddHeader("Content-Disposition", "attachment;filename=" & "TextFile1.txt")
Response.ContentType = "text/plain"
Response.WriteFile("TextFile1.txt")
So, the donloaded file will have TextFile.txt contents + the HTML content of Page.aspx
Yes. I also have the same problem.
How do you force download on MAC IE? Can anyone help?
Hi, I have no problem forcing the download box to show up with ASP, but I want to do more with it. The download box has 4 buttons, OPEN, SAVE, CANCEL and HELP. I'd like to know what the user selected and do something different. Specifically, if the user click OPEN OR SAVE, I want to increment a counter of how many times the file has been downloaded. If the user click CANCEL, I want to do nothing.
My question is: How can I do that? Thanks.
P.S. Here is the code that I use to force download box:
Response.Buffer = True
Response.Clear
Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
Response.AddHeader "Content-Length", objFile.Size
Response.ContentType = "application/octet-stream"
Response.CharSet = "UTF-8"
'...
' code to write out binary stream of the file to browser buffer
'...
Response.Flush
I used the given code and used it to invoke a file download box.. It worked also and when I gave a location and saved the file.. the file got saved (with the same name) but it was empty... that is no content... please help out
This worked for me:
response.addheader "content-disposition","attachment; filename='" & Filename & "'"
server.transfer Filename
hi, all
i understand the use of ADDHeader method, but there's a slight problem with this.....
got a page (page1.asp) from which the user selects certain values and on click of a button (page2.asp) is displayed andthe results are shown in a HTML table.... so far so good.
Page2.asp has a link for downloading the filein Pipe format or Excel..... now what needs to be done is once the link is clicked the file needs to generated and prompted for "Save" .....
i tried adding the Header but then it asks to save the (page2.asp) itself......
can this be solved
Cheers !!!
Niraj
[1]How do I force a file download from a MAC without the file opening in the browser?[/1]
I am experiencing a similar problem with downloads on mac.
Can you point me to the articles you found.
Regards
Here's an example:
[courier new]
<% @LANGUAGE = VBScript %>
<%
Option Explicit
Response.AddHeader "content-disposition","attachment; filename='test.xls'"
%>
<html>
<head>
<title>Save file</title>
</head>
<body>
<HREF="test.xls">TEST.XLS</A>
</body>
</html>
[/courier new]
I've read articles which stated that the Mac IE 5.1 is at fault here.
Need to get Microsoft to fix this!!!!!
I am having same problem on Mac. Could not find any help on this so far. Could you please reply if you found a solution.
Use the filesystem object. It is MUCH slower than ADO stream, but it will get the job done.
How can the download be forced if the link to the file is known: that is the file is being downloaded to client's machine from a remote server besides mine.
I have tried using "ADODB.stream" object to do this. But, my server has ADO 2.1 and ADO 2.5 or higher is needed for the stream object to work. I am not allowed to upgrade the ADO on this server. Your urgent help is greatly appreciated.
May someone please help me with force download problem. I need this urgently. I greatly appreciate your help.
How can the download be forced if the link to the file is known: that is the file is being downloaded to client's machine from a remote server besides mine.
I have tried using "ADODB.stream" object to do this. But, my server has ADO 2.1 and ADO 2.5 or higher is needed for the stream object to work. I am not allowed to upgrade the ADO on this server. Your urgent help is greatly appreciated.
Hi,
I tried your tutorial. Works great on Windows and IE 5.5+. But my Mac running IE 5.1 doesn't work. The Save As dialog did not show up on Mac.
Please help.
This thread is for discussions of Force 'file download' for known file types.