Javascript 通过js或query强制下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5192917/
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-08-23 16:05:59  来源:igfitidea点击:

Force download through js or query

javascriptjquery

提问by Shaun

Is it possible to force a download through JS or Javascript i-e the web page should not open the file in new tab in the browser but to pop up to let the user to choose eith "save as" or open with ???

是否可以通过 JS 或 Javascript 强制下载,即网页不应该在浏览器的新选项卡中打开文件,而是弹出让用户选择“另存为”或用 ???

采纳答案by Sean Patrick Floyd

You can not force that behavior from JavaScript, the HTTP Headers need to be set on the server side:

您不能从 JavaScript 强制执行该行为,需要在服务器端设置 HTTP 标头:

Content-disposition=attachment; filename=some.file.name

The way you can solve the problem is to let your AJAX method redirect the user to the URL of the PDF:

解决问题的方法是让您的 AJAX 方法将用户重定向到 PDF 的 URL:

location.replace('path/to.pdf');

(The above HTTP headers must be set for the PDF)

(必须为 PDF 设置上述 HTTP 标头)



Update

更新

At the time of this answer, it wasn't possible. Now it is, scroll down to see the other answer saying so.

在这个答案的时候,这是不可能的。现在是这样,向下滚动以查看其他答案

回答by Gokul N K

With the advent of HTML5 you could just use the new property download in the anchor tag.

随着 HTML5 的出现,您可以在锚标记中使用新的属性下载。

The code will look something like

代码看起来像

<a download="name_of_downloaded_file" href="path/to/the/download/file"> Clicking on this link will force download the file</a>

It works on firefox and chrome latest version. Should I mention that I didn't check it in IE? :P

它适用于 Firefox 和 chrome 最新版本。我应该提到我没有在 IE 中检查它吗?:P

Edited the download attribute after comment from sstur

在 sstur 发表评论后编辑了下载属性



https://caniuse.com/#feat=download

https://caniuse.com/#feat=download

回答by Lukas Liesis

dynamic create link and click it with download attribute for force download as file:

动态创建链接并单击它的下载属性以强制下载为文件:

var anchor = document.createElement('a');
anchor.href = this.props.download_url;
anchor.target = '_blank';
anchor.download = this.props.file_name;
anchor.click();

Take a notice that i didn't even added it to DOM, so it's fast.

请注意,我什至没有将它添加到 DOM,所以它很快。

P.S downloadattribute won't work with IE. But it will just open link in new tab. http://caniuse.com/#feat=download

PSdownload属性不适用于 IE。但它只会在新标签中打开链接。 http://caniuse.com/#feat=download

回答by Darin Dimitrov

No, it is not possible and thanks God it isn't. Otherwise I leave you to the imagination of what kind of files could be stored on your computer when you visit a web site without you knowing it.

不,这是不可能的,感谢上帝它不是。否则,当您在不知情的情况下访问网站时,我会让您想象一下,什么样的文件可以存储在您的计算机上。

As @Paul D. White pointed out in the comments section if you want to open the file inline (inside the browser) with the default program associated with it you could have the server send the Content-Disposition HTTP header. For example:

正如@Paul D. White 在评论部分指出的那样,如果您想使用与其关联的默认程序内联(在浏览器内)打开文件,您可以让服务器发送 Content-Disposition HTTP 标头。例如:

Content-Disposition: inline; filename=foo.pdf

回答by Daniel Kutik

No this is not possible with JQuery/JavaScript only.

不,这仅在 JQuery/JavaScript 中是不可能的。

You will need a server side script which returns you the file with a Content-Type(HTTP Header) which will force the browser to download your requested file. An possible value for Content-Typewould be application/force-download.

您将需要一个服务器端脚本,该脚本会返回带有Content-Type(HTTP 标头)的文件,这将强制浏览器下载您请求的文件。一个可能的值Content-Typeapplication/force-download