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
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Variable In Vb.Net
by chia (0 replies)
-
ideas in building a captive portal
by sjranjan (2 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
who can help me to dowload wallhack if u help me add me in YM darylljames_gorgonio@yahoo.com
!--removed tag-->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.
Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.
Thanks
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
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]
Can you point me to the articles you found.
Regards
[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.