简体   繁体   中英

PDF download icon at right corner not working in browser, Opening PDF String in new window with javascript?

When I click the download icon on the right corner, nothing happen. How to make it downloadable?

The following code i have used for PDF preview.

let pdfWindow = window.open("", "_blank")
pdfWindow.document.write("<iframe src='data:application/pdf;base64, " + yourDocumentBase64VarHere+"' width='100%' height='100%'  allowfullscreen></iframe>")

This works for me in all browsers!

viewPdf(base64String) {
    let byteCharacters = atob(base64String);
    let byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    let byteArray = new Uint8Array(byteNumbers);
    let blob = new Blob([byteArray], { type: 'application/pdf' });

    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
       window.navigator.msSaveOrOpenBlob(blob, 'Report.pdf');
    }
    else {
       var fileURL = URL.createObjectURL(blob);
       window.open(fileURL);
    }
}

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