Solved

Force browser to prompt save file from link

Posted on 2001-06-16
13
206 Views
Last Modified: 2008-03-10
I am wondering if anyone has found a way to force the browser to prompt to download a file directly from a form button.  I understand that this might not be possible, and also know you can always tell them to right click on the link and choose "Save File to Disk" or whatever, but I'm displaying the file in one frame and want them to be able to click on a button to download the file if they wish.  Any thoughts would be appreciated.

Thanks,

-Jon
0
Comment
Question by:punkrider
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 6199006
You can force the browser to do anything, if the client size settings are to open directly then that is what the client will do.

The usual trick to work round this is to put the file in a zip archive as almost all browsers are set to prompt for zip files.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6199018
Wow! lot of B grades.  

Dorward is correct zip is the most reliable solution.

Though exe extension will also do it.

Cd&
0
 

Expert Comment

by:andreas_boehmer
ID: 6199096
So I presume what you want the user to download is an image? Yeah, I agree with dorward and cobol, you'll need to stick it in a zip-format. If you still want to use a button to download it, do something like this:


1. In the <head>:
<script langauge="javascript">
     
   function download(Filename){
   document.location="http://www.yourDomain.com/"+Filename;
          }
     
</script>



2. And in the body:
<form action="javascript: download();">
<input type="button" onclick="download('YourFile.zip');" value="Download File">
</form>
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6199145
If you have server control, you can set it up to send back an unrecognised mime type header with the file, so the browser prompts, but the file doesn't need to change.
0
 
LVL 9

Accepted Solution

by:
TTom earned 100 total points
ID: 6199159
If you are on a Microsoft server, you can probably set up your link as an ASP page and feed the image down from the server with a response.header that indicates the file should be downloaded.

<%
Response.ContentType = "any/any"
Response.AddHeader "Content-disposition", "attachment; filename=" & YourDesiredFileNameAndExtension
%>

You would then need to write the file contents.  I am less familiar with that process.

Tom
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6200054
I've just had a quick play with this, and unfortunately I think it's not going to be very easy because:
- The file will be downloaded with the name of the asp page
- The fileSystemObject doesn't work with binary files.

Sorry for misleading you on this one. Another alternative (using IIS) is to map the mime type 'incorrectly' on your server for a specific virtual directory mapped to the same directory - then when you link to the file, if you link via the virtual directory you'll get a download prompt.

Eg in IIS4 to make gifs download by default (just having a quick, unautorised play on my live server...)
Create a new virtual directory called downloadgif or whatever. Point the directory to the same as your main app. Edit the MIME properties -> HTTP Headers -> MIME Map File Types
Add a new type (or update the existing one) for gif with an unrecognised content type of something/whatever
Now to download the gif file, rather than displaying it, just link to downloadgif/myfile.gif

Incidentally, the client browser will still display these 'munged' types correctly in IMG tags.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:punkrider
ID: 6204426
I am running IIS on a win2k server.  The last two answers are very good, but I'm not dealing soley with image files.  Would either of your ideas work with all types of files?  I would rather not have to create virtual folders for each file, as there are several thousand files each in it's own PK folder.  I'm going to play around with each and I'll be back.  

To clarify the situation... Users upload files into their web space, when they go to view their files in my javascript file manager they click on the link to the file.  I would also like to have a little image next to the link that lets them download the file instead of viewing it.  

IE

(Save file foo.bar) (View file foo.bar)
0
 
LVL 9

Expert Comment

by:TTom
ID: 6204462
The content disposition and header should work with any type of file.  The problem will be that you would have to create an ASP for each file in order to achieve this effect.  You MIGHT be able to automate this process by doing a loop through the directory using the FileSystemObject.

Not sure, though.

Tom
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6204472
You shouldn't need a separate directory for each file, just one virtual directory pointing to the root of the same webspace - the folders will be the same, but you can set the MIME types differently. Unfortunately you'll have to remap all of the registered MIME types you want to be able to dowload which will be a chore.
Keep us informed how you get on with this one.
0
 

Author Comment

by:punkrider
ID: 6204563
I'm playing with this a bit... also looking into ISAPI filters or extensions to write raw binary to the browser.  I'll keep you informed... points will not go to waste, and might even increase!
0
 

Author Comment

by:punkrider
ID: 6211492
I have found an answer here...

http://support.microsoft.com/support/kb/articles/Q193/9/98.ASP

By creating an ActiveX DLL to do the binary reading of the file, in conjunction with TTom's ASP header info, I was able to prompt the browser to download and then save the file.  The only problem with my code is the Netscape does not put the correct filename in the "Save As..." dialog.  IE does, but through another header.  I'm still playing around so maybe it's just something I missed, but here's the code I have so far:

------------BEGIN CODE SAMPLE------------------------

<%@ Language=VBScript %>
<%

strFileName = "C:\test.txt"

strDownloadFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))

Response.Buffer = True
Response.ContentType = "application/octet-stream"
Response.AddHeader "Document", strDownloadFileName
Response.AddHeader "Script_Name", strDownloadFileName


Response.AddHeader "Content-Type", "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename = " & strDownloadFileName

Dim vntStream

Set oMyObject = Server.CreateObject("FileBinRead.BinRead")
vntStream = oMyObject.readBinFile(strFileName)

Response.BinaryWrite vntStream

Set oMyObject = Nothing

Response.End

%>

--------------END CODE SAMPLE--------------------------

The FileBinRead object was the ActiveX DLL that I got from the MSDN article.  Check it out.

Thanks TTom,

-Jon
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6211638
Thanks for posting the code - Very useful - typical of Netscrap not to understand standard headers, but there you go. Let us know if you find a Netscrap workaround.
0
 

Author Comment

by:punkrider
ID: 6212376
This seems to work with both browsers... Must be netscape needs the double quotes or something. Thanks again to everyone.

<%@ Language=VBScript %>

<%

strFileName = "C:\test.txt"

strDownloadFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))

Response.Buffer = True

Response.ContentType = "application/octet-stream; name="""&strDownloadFileName & """"
Response.AddHeader "Content-disposition", "attachment; filename="""& strDownloadFileName & """"

Dim vntStream

Set oMyObject = Server.CreateObject("FileBinRead.BinRead")
vntStream = oMyObject.readBinFile(strFileName)

Response.BinaryWrite vntStream

Set oMyObject = Nothing

Response.End

%>
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now