3

"wget http://www.example.com/file.doc" downloads that file to the local disk.

What is the equivalent of the above in javascript? for example, consider the following html snippet.

<html>
<head>
   <script language="JavaScript">
      function download_file() {
         var url = "http://www.example.com/file.doc"
         //
         // Question: 
         //
         // what should be done here to download 
         // the file in the url?
         //
      }
   </script>
</head>
<body>
   <input type="button" value="Download" onclick="download_file()">
</body>
</html>

Please suggest a solution that is compliant with all the browsers.

Sangeeth.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • This is answered here: http://stackoverflow.com/questions/349067/download-a-file-using-javascript – jzila Nov 23 '11 at 02:50

3 Answers3

3

After a exploring more than a month, with a help of my friend, we were able to find out the following.

The website where the file is hosted is not allowing us to download the file using window.location = url; or window.open(url);

Finally we had to use the data-downloadurl support from HTML5 as follows

<a href="<url-goes-here>" data-downloadurl="audio/mpeg:<filename-goes-here>:<url-goes-here>" download="<filename-goes-here>">Click here to download the file</a>

We embed this html into the host html and when clicked on the link, it triggers the download.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
1

Why not use:

 function download_file() {
   var url = "http://www.example.com/file.doc"
   window.location = url;
 }

See https://developer.mozilla.org/en/DOM/window.location

If you need to open this in a new window/tab first then use:

 function download_file() {
   var url = "http://www.example.com/file.doc"
   window.open(url);
 }

See https://developer.mozilla.org/en/DOM/window.open

hafichuk
  • 10,351
  • 10
  • 38
  • 53
  • window.location is something I learnt today. Thanks! But this is not helping me in downloading the file from the URL to my hdd. This just opens the file in the browser. What I'm looking for is something similar to right-click & "Save Link As.." option. Any suggestions? – Sangeeth Saravanaraj Nov 23 '11 at 14:05
  • Well it seems that this is going to be something quite dependent on your setup. Most webservers send *.doc files as attachments by setting the http response header [`Content-Disposition: attachment; filename=file.doc`](http://en.wikipedia.org/wiki/List_of_HTTP_header_fields). It's up to the browser to display the dialog box or not using this as a hint. Maybe you have a MS office plugin enabled that you just need to disable. – hafichuk Nov 23 '11 at 14:31
0

First thing that always comes in mind of every answerer to this question is executing wget shell command from java script.I'm almost certain that that's not possible because of major security risk.

You pretty much need to have ajax which sends command to command line either through php, or another scripting language via ajax...

You could probably make that happen with something like http://www.phantomjs.org/
I am saying probably because I read it from somewhere.

Wazy
  • 8,822
  • 10
  • 53
  • 98
  • Executing `wget` from my browser... never even entered my mind as a possibility. Just set it as a link, or the document's href, and it'll prompt you to download it. – Dave Newton Nov 23 '11 at 03:01
  • I didnt said Executing `wget` from my browser – Wazy Nov 23 '11 at 03:03
  • Even though the OP clearly wants to download it from a web page? Interesting approach, then, not using a browser. – Dave Newton Nov 23 '11 at 03:06
  • Looking at http://code.google.com/p/phantomjs/wiki/QuickStart, downloading a file from the given URL seems possible.. But I'm looking for a JavaScript equivalent of that! – Sangeeth Saravanaraj Nov 23 '11 at 14:09
  • I've tried various mime types f.e. "data:text/attachment", "Content-disposition:attachment", "application/octet-stream", etc with window.open(), window.location.href, etc but no luck. I've searched stackoverflow and found that many people had come across the similar situation and no one have a convincing answer! .. I'm going to continue looking for a solution. – Sangeeth Saravanaraj Nov 25 '11 at 02:38