简体   繁体   中英

Javascript trigger download outside of browser

What would be the best way of triggering a download outside of the browser?

I have a Html Page which is not on a Server but should only function as a Digital Signage Viewer. The Server (which already exists) sends a playlist over TCP Sockets (the server is a bit older) telling the client what media it needs to download from where and where the media is supposed to be presented.

The communication between the Javascript Client code and the Tomcat server is irrelevant for now. What I would like to know is if there is a way to download the files to a certain directory without letting the browser download the file (Problems -> Download Pop Up -> Confirmation -> Can't preset different download folders etc. with Javascript), so that I can set the source of the media tags after they have downloaded ?

(If you're asking why I'm not simply streaming the media it's because if the connection is lost the media still needs to continue to play)

I really hope my explanation makes any sense.

Cheers Cris

You may send the content to a web server and return it back to browser with specified type/filename. For example in PHP you may do something like this:

header('Content-type: text/html');
header('Content-disposition: attachment;filename=myfile.txt'); 

Another option is this nice JavaaScript library which allows you to save some contents on client-side: https://github.com/koffsyrup/FileSaver.js

It allows you to do something like this in browser:

saveTextAs("Hi,This,is,a,CSV,File", "test.csv"); 

You may also want to store your contents in browser's localStorage or sessionStorage .

What I would like to know is if there is a way to download the files to a certain directory without letting the browser download the file

If this would be possible it would open a security leak because it would make it possible to put special files (like programs) into special folders (like autostart) and then cause unwanted execution of code. This this is hopefully not possible due to security concerns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM