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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7763505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:13:48  来源:igfitidea点击:

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

javascriptdownload

提问by Frederik.L

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. Ultimately, I would like to do it for HTML documents too. The main idea is to make it easy for users to download files without asking them to navigate into the context menu.

我想允许用户通过单击直接下载文件。然而,当涉及到已知的模因类型(如 HTML、音频、视频等)时,存在一个问题。理想情况下,我想触发音频/视频文件的下载提示。最终,我也想为 HTML 文档做这件事。主要思想是让用户可以轻松下载文件,而无需要求他们导航到上下文菜单。

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. Whenever you are inside a plugin or injected script context (i.e. developing a plugin for Firefox, Chrome or Safari), you may want to avoid asking for a server-side response.

我寻找 JavaScript 解决方案的原因是 PHP 方法仅在您处于网站上下文中时才有效。每当您在插件或注入的脚本上下文中(即为 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 Downloadifywhich 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 方式来触发下载提示,以便用户可以使用简单的左键单击直接下载文件?

回答by ebidel

There is a new downloadattribute in HTML5 that you can annote links with. 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.

downloadHTML5 中有一个新属性,您可以用它来注释链接。它向浏览器指示应该下载资源而不是导航到该资源。目前,它仅适用于 Chrome,但它是 HTML 规范的一部分,有望很快被其他浏览器采用。

Demo: http://html5-demos.appspot.com/static/a.download.htmlMore 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

回答by Roberto Tronci

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 和旧浏览器的情况下)该链接将打开另一个带有要下载文件的选项卡窗口。

回答by Donald Duck

You can use <a href="example" download>. This is HTML5 and it works with Chrome, Firefox and Edge (but not with Internet Explorer, not even modern versions).

您可以使用<a href="example" download>. 这是 HTML5,它适用于 Chrome、Firefox 和 Edge(但不适用于 Internet Explorer,甚至不适用于现代版本)。