使用 javascript 启动下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3644938/
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
Initiating a download with javascript
提问by kzip
I need to dynamically initiate a download with javascript. I have seen how people do this by doing something like
我需要使用 javascript 动态启动下载。我已经看到人们如何通过做类似的事情来做到这一点
window.open("some url", "Download");
but I need to do it without changing the url of the current page (and not using frames if I can help it, or created and destroying a frame dynamically). Anybody know how to do this?
但我需要在不更改当前页面的 url 的情况下执行此操作(如果可以帮助,则不使用框架,或者动态创建和销毁框架)。有人知道怎么做吗?
回答by BalusC
You don't need window.open(). It's plain ugly and prone to popupblockers (where you have no control over in clients). Just window.locationis sufficient if the response header of the requested download URL contains Content-Disposition: attachment. This won't change the current URL in the browser address bar nor the current page, but just pop a Save Asdialogue.
你不需要window.open(). 它非常丑陋并且容易出现弹出窗口阻止程序(您无法控制客户端)。只要window.location是足够的,如果被请求的下载URL的响应头包含Content-Disposition: attachment。这不会更改浏览器地址栏中的当前 URL 或当前页面,而只是弹出另存为对话框。
E.g.
例如
window.location = 'http://download.winzip.com/winzip145.exe';

