简体   繁体   English

是否可以仅使用JavaScript(客户端方法)在浏览器中为识别的MIME类型启动下载提示?

[英]Is it possible to initiate a download prompt in the browser for recognized MIME types using only JavaScript (client-side approach)?

I would like to allow the user to directly download a file with a single click. 我想允许用户单击一下直接下载文件。 There is however a problem when it comes to known meme-types like HTML, audio, video, etc. Ideally, I would like to trigger a download prompt for audio/video files. 但是,当涉及到已知的模因类型(例如HTML,音频,视频等)时,就会出现问题。理想情况下,我想触发音频/视频文件的下载提示。 Ultimately, I would like to do it for HTML documents too. 最终,我也想对HTML文档执行此操作。 The main idea is to make it easy for users to download files without asking them to navigate into the context menu. 主要思想是使用户可以轻松下载文件,而无需要求他们导航到上下文菜单。

I think for example to people that are not really comfortable with a computer and its main functions. 我认为例如对于不太喜欢计算机及其主要功能的人。 These people will surely prefer a better way than "save as". 这些人肯定会比“另存为”更喜欢更好的方法。

The reason why I am looking for a JavaScript solution is that the PHP approach only works if you are in a web site context. 我正在寻找JavaScript解决方案的原因是,PHP方法仅在网站环境中才有效。 Whenever you are inside a plugin or injected script context (ie developing a plugin for Firefox, Chrome or Safari), you may want to avoid asking for a server-side response. 每当您位于插件或注入的脚本上下文中(即为Firefox,Chrome或Safari开发插件)时,您可能都希望避免请求服务器端响应。

I tried to achieve this with window.open() and document.execCommand("saveAs",... . It does work, although it is glitchy and fails for huge files. 我试图通过window.open()document.execCommand("saveAs",...来实现这一点,尽管它有故障并且无法处理大文件,但它确实可以工作。

Then, I tried Downloadify which does not work in every situations. 然后,我尝试了Downloadify ,它在每种情况下都无法正常工作。

Is there a pure JavaScript, no Ajax way to trigger a download prompt so the user can directly download a file using a simple left click? 是否有纯JavaScript,没有Ajax方式触发下载提示,以便用户可以通过简单的左键单击直接下载文件?

There is a new download attribute in HTML5 that you can annote links with. HTML5中有一个新的download属性,您可以使用它注释链接。 It indicates to the browser that the resource should be downloaded rather than navigated to. 它向浏览器指示应该下载而不是导航到资源。 Right now, it only works in Chrome, but it is part of the HTML spec and will hopefully be adopted by other browser soon. 目前,它仅适用于Chrome,但它是HTML规范的一部分,并有望很快被其他浏览器采用。

Demo: http://html5-demos.appspot.com/static/a.download.html More info: http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download 演示: http : //html5-demos.appspot.com/static/a.download.html更多信息: http : //updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

You can use <a href="example" download> . 您可以使用<a href="example" download> This is HTML5 and it works with Chrome, Firefox and Edge (but not with Internet Explorer, not even modern versions). 这是HTML5,适用于Chrome,Firefox和Edge(但不适用于Internet Explorer,甚至不是现代版本)。

If someone reaches this question now the best solution is 如果有人遇到这个问题,最好的解决方案是

<a href="example" download target="_blank">

If the browser supports the HTML5 attribute download will start the download of the file, otherwise (in case of Internet Explorer and old browsers) the link will open another tab window with the file to download. 如果浏览器支持HTML5属性,则下载将开始下载文件,否则(对于Internet Explorer和旧的浏览器),该链接将打开另一个带有要下载文件的选项卡窗口。

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

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