How to download a file in javascript?

Force Download (save as) Binary File (i.e. Word) - Windows and Mac Compatible

  • Developing web pages in .asp with several links to word documents residing on a secure server. Want to force download/save as dialogue instead of opening document, since opening requires server user name/password. Don't want to use zip, altering file extension etc. Have already tried using "content-disposition" method. Works great on PCs (although I hear a bug in IE 5.5 causes problems) but I believe this method will not be MAC compatible. Javascript etc are options.

  • Answer:

    Hi Jon, There is no magic solution to what you are trying to accomplish. Before I suggest a few methods to consider (although none of them will give you the result you are looking for 100% of the time), here is an explanation as to why there is not such feature that would work on every system: The web protocols were designed to identify the MIME Content-type headers (type of content data), but not what task should be performed on them once a header is identified (source + more info at: http://webtips.dantobias.com/force.html ). Defining what should be done with the content type once it is identified would reduce flexibility. Because each system is different, a lot of systems would be unable to do what is requested of them. This type of dilemma has been around for a long time in other forms. For example, did you know that using the <BOLD> tags might not work on some older systems? The reason is simple: BOLD tells the browser in a very specific way HOW to process the tag. If your web browser does not understand any of the commands it will give up, not knowing what to do. An alternate tag, which will accomplish the same job even on older browsers, is <STRONG>. This tag tells the browser of what the final result should be, not how it should be created. The browser is than set free to accomplish its job in a way it has been told to do it. <BOLD> takes the control away from the web browser and not only tells it WHAT to do, but also HOW to do it. Not specifying how to handle the data on the remote system also lowers the security risks (so, i.e., .exe files don't execute themselves upon transfer). So, main reason for not being able to do what you are asking with a 100% success rate is security and compatibility. Now, this is the part where you might find a little more favourable information. It deals with a few ways of accomplishing your task with as much compatibility as possible: Try using the application/octet-stream for your MIME type. The web site, which suggests this method, mentions MACs. Supposedly this is one of the better options. ASP code to accomplish your task can be downloaded from http://www.asp101.com/articles/jason/filedownloadlist/filedownloadlist.zip Another ASP example: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=4&txtCodeId=7351 The ASP examples are from http://4guysfromrolla.aspin.com/home/tutorial/file/updownlo?cob=4guysfromrolla where you will find about 30 file transfer utilities (not all for downloading). You might want to browser through there in case there is something better I might have missed. Now on the more difficult spectrum, VBScript download force: http://www.morningz.com/mz/crystal/safilemgr/safilemgr.asp Most browser allow download forcing by holding the SHIFT key when clicking the file. There is some popular browsers, which DON'T support this feature. I hope that between these methods you will find something that serves you with content (since the perfect solution does not exist, but than again the simplest of things on the web are not compatible with everyone out there). Thank you, and best of luck to you. Slawek

jonbliss-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.