简体   繁体   English

如何在不打开浏览器的情况下下载pdf文件

[英]How to download pdf file without opening the browser

This code i am trying but without any error it not showing anything for me. 我正在尝试此代码,但没有任何错误,它没有为我显示任何内容。

if (!window.ActiveXObject) {
    var save = document.createElement('a');
    save.href = fileURL;
    save.target = '_blank';
    save.download = fileName || 'unknown';

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
}

// for IE
else if ( !! window.ActiveXObject && document.execCommand)     {
    var _window = window.open(fileURL, '_blank');
    _window.document.close();
    _window.document.execCommand('SaveAs', true, fileName || fileURL)
    _window.close();
}

This is not working for me 这对我不起作用

Try appending save element to document.body using .appendChild() before calling event.initEvent('click', true, true) , save.dispatchEvent(event) 在调用event.initEvent('click', true, true)save.dispatchEvent(event)之前,尝试使用.appendChild()save元素追加到document.body

 var fileURL = "data:text/plain,abc", fileName = "file.txt"; if (!window.ActiveXObject) { var save = document.createElement('a'); save.href = fileURL; save.target = '_blank'; save.download = fileName || 'unknown'; var event = document.createEvent('Event'); // append `a` element : `save` to `document.body` here document.body.appendChild(save); event.initEvent('click', true, true); save.dispatchEvent(event); // (window.URL || window.webkitURL).revokeObjectURL(save.href); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM