简体   繁体   中英

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)

 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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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