12

I was wondering if there was any method to implement browser's download file prompt using JavaScript.

My reason - well users will be uploading files to a local fileserver which cannot be accessed from the webserver. In other words, both will be on different domains!

For example, let’s say websites hosted on www.xyz.com, but files would reside on local file server with address like \\10.10.10.01\Files\file.txt. How am I uploading/transferring file to local fileserver... using ActiveX and VBscript! (don’t ask :-)

So I am storing local file path in my database and binding that data to a grid. When the user clicks on that link, the file opens in a window (using JavaScript).

Problem is certain file types like text, jpg, pdf, etc. open inside browser window. How would I be able to implement content-type or content-disposition using client side scripting? Is that even possible?

EDIT: the local file server has a window's shared folder on which the files are saved.

casperOne
  • 73,706
  • 19
  • 184
  • 253
aunlead
  • 985
  • 2
  • 9
  • 17
  • What kind of server is your 'local file server'? An actual HTTP/FTP/whatever server or are you talking about Windows' shared folders (ie did you mean `\\10.10.10.01` instead of `//10.10.10.01`? – Christoph Apr 07 '09 at 19:19
  • Its a shared folder in windows & actual path to file would be '\\10.10.10.01\Files\file.txt' – aunlead Apr 08 '09 at 10:26
  • @aix: then you're out of luck - there's no way to sent the appropriate meta-information via HTTP headers – Christoph Apr 08 '09 at 15:48
  • I am pretty sure there is no way to do it with javascript. – Konstantin Tarkus Apr 07 '09 at 19:00
  • :-\ well tried this http://www.webdeveloper.com/forum/showpost.php?p=74189&postcount=3 but no luck... – aunlead Apr 08 '09 at 10:31
  • Take a look at [this article](http://support.microsoft.com/kb/260519) on content-disposition. As said, it has to be set in the response header, and isn't a Javascript implementation. – Alex Rozanski Apr 07 '09 at 19:07

4 Answers4

6

"content-disposition: attachment" is pretty much the only way to force that, and this MUST be set in the response header.

bdukes
  • 152,002
  • 23
  • 148
  • 175
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
  • How can i set it on the client side? Currently i am using javascript to open the file. Calling window.open('\\10.10.10.01\Files\file.txt') to open the file. This would open the file in a window. Isnt "content-disposition: attachment" a server side response header setting? – aunlead Apr 08 '09 at 10:29
  • As the question said "this must be set in the response header", you can't set it on the client. – Quentin Apr 01 '10 at 10:38
5

If the file is hosted on a web server like in your example, you can do:

window.location.replace(fileUrl);

.. and the browser will figure out what to do with the file. This works great for most files, such as .xls, .csv, etc, but keep in mind that this isn't full-proof because the user's MIME handler settings will determine what to do with the file... i.e. if it is a .txt file it will most likely just be displayed in the browser and will not be given a "file download" dialogue box.

Vince
  • 51
  • 1
  • 1
  • Note that this means you can put this in the address bar: `data:text/csv,a,b,c%0ad,e,f` (this creates a CSV file download with 3 columns and 2 rows). Not sure this is in line with the original question, but googling how to create that prompt, I ended up here. Perhaps it helps future readers :) – Luc May 30 '16 at 16:53
4

As of August 2015, adding the "download" attribute to your tag enables the behavior you're looking for, at least in Chrome.

Ian Ni-Lewis
  • 2,377
  • 20
  • 20
  • 3
    Unfortunatly not cross-origin support on Firefox and not working on Safari, Edge and IE, that fact make download attribute useless for me :/ – Sojtin Aug 31 '15 at 08:51
2

You could try using a plain hyperlink with type="application/octet-stream". Seems to work in FF, but IE and Opera ignore the attribute.

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • By spec, IE and Opera are right. The type attribute says "Expect this from the server" so that clients which don't understand a particular content type can avoid bothering to try to download it (most useful with bots). The real Content-Type trumps it. – Quentin Apr 01 '10 at 10:39
  • In my opinion, IE and Opera are wrong: in absence of any other content-type information (which is the case here), the 'advisory hint' (HTML spec) given by the type atttribute should be respected by the browser; remeber, the file isn't sent via HTTP, so there's no Content-Type field which could trump the attribute – Christoph Apr 01 '10 at 20:32