Browser download file prompt using JavaScript
Asked Answered
E

4

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.

Emmert answered 7/4, 2009 at 18:57 Comment(6)
I am pretty sure there is no way to do it with javascript.Oxidize
Take a look at this article on content-disposition. As said, it has to be set in the response header, and isn't a Javascript implementation.Oxidation
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?Wireman
Its a shared folder in windows & actual path to file would be '\\10.10.10.01\Files\file.txt'Emmert
:-\ well tried this webdeveloper.com/forum/showpost.php?p=74189&postcount=3 but no luck...Emmert
@aix: then you're out of luck - there's no way to sent the appropriate meta-information via HTTP headersWireman
M
6

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

Moseley answered 7/4, 2009 at 19:5 Comment(2)
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?Emmert
As the question said "this must be set in the response header", you can't set it on the client.Heligoland
I
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.

Invariable answered 7/9, 2011 at 16:5 Comment(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 :)Nathanaelnathanial
I
4

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

Italicize answered 3/8, 2015 at 16:24 Comment(1)
Unfortunatly not cross-origin support on Firefox and not working on Safari, Edge and IE, that fact make download attribute useless for me :/Stultify
W
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.

Wireman answered 7/4, 2009 at 19:12 Comment(2)
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.Heligoland
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 attributeWireman

© 2022 - 2024 — McMap. All rights reserved.