Force browser to prompt save file from link

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
punkriderAsked:
Who is Participating?
 
TTomConnect With a Mentor Commented:
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
 
dorwardCommented:
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
 
COBOLdinosaurCommented:
Wow! lot of B grades.  

Dorward is correct zip is the most reliable solution.

Though exe extension will also do it.

Cd&
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
andreas_boehmerCommented:
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
 
andyclapCommented:
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
 
andyclapCommented:
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
 
punkriderAuthor Commented:
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
 
TTomCommented:
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
 
andyclapCommented:
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
 
punkriderAuthor Commented:
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
 
punkriderAuthor Commented:
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
 
andyclapCommented:
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
 
punkriderAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.