Use an invisible <iframe>
:
(使用不可见的<iframe>
:)
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file's MIME Type to a nonsensical value, such as application/x-please-download-me
or alternatively application/octet-stream
, which is used for arbitrary binary data.
(要强制浏览器下载文件,否则它将能够呈现(例如HTML或文本文件),您需要服务器将文件的MIME类型设置为无意义的值,例如application/x-please-download-me
或者application/octet-stream
,用于任意二进制数据。)
If you only want to open it in a new tab, the only way to do this is for the user to a click on a link with its target
attribute set to _blank
.
(如果您只想在新选项卡中打开它,唯一的方法是让用户单击链接并将其target
属性设置为_blank
。)
In jQuery:
(在jQuery中:)
$('a#someID').attr({target: '_blank',
href : 'http://localhost/directory/file.pdf'});
Whenever that link is clicked, it will download the file in a new tab/window.
(只要单击该链接,它就会在新的选项卡/窗口中下载该文件。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…