使用不可见的<iframe>
:
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
要强制浏览器下载本来可以呈现的文件(例如HTML或文本文件),则需要服务器将文件的MIME类型设置为无意义的值,例如application/x-please-download-me
或另外application/octet-stream
,用于任意二进制数据。
如果只想在新标签页中打开它,则唯一的方法是让用户单击其target
属性设置为的链接_blank
。
$('a#someID').attr({target: '_blank',
href : 'http://localhost/directory/file.pdf'});