简体   繁体   English

在浏览器外部下载Javascript触发器

[英]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. 我有一个不在服务器上的HTML页面,而应仅用作数字标牌查看器。 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. 服务器(已经存在)通过TCP套接字发送播放列表(服务器较旧),告诉客户端需要从何处以及应该从何处下载该媒体。

The communication between the Javascript Client code and the Tomcat server is irrelevant for now. 目前,Javascript客户端代码与Tomcat服务器之间的通信是不相关的。 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 ? 我想知道的是,是否有一种方法可以在不让浏览器下载文件的情况下将文件下载到某个目录(问题->下载弹出窗口->确认->无法使用Javascript预设其他下载文件夹等) ),这样我就可以在媒体标签下载后设置其来源?

(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. 您可以将内容发送到Web服务器,然后将其返回给具有指定类型/文件名的浏览器。 For example in PHP you may do something like this: 例如,在PHP中,您可以执行以下操作:

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 另一个选择是这个漂亮的JavaaScript库,它允许您在客户端保存一些内容: 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 . 您可能还希望将内容存储在浏览器的localStoragesessionStorage

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. 出于安全考虑,希望这不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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