1

PROBLEM

My PDF download works in Chrome, but does not work in IE 11/10/9.

In IE, it will prompt me a security warning, but after I click Yes to allow it, nothing happens.

CODE

handleDownload = (reportLoc) => {
        var path = reportLoc.item.ReportPath;
        $.get("/Api/Apps/GetFileFromServer?filePath=" + path, (response) => {
            this.setState({ base64EncodedPDF: response });
        }).then(() => {
            let a = document.createElement("a");
            a.href = "data:application/octet-stream;base64," + this.state.base64EncodedPDF;
            a.download = path.substring(path.lastIndexOf("\\") + 1);
            a.click();
            });

    }

Solutions to make it work in both Chrome and IE?

xzk
  • 827
  • 2
  • 18
  • 43
  • And what exactly is that warning saying? – CBroe Jun 11 '18 at 09:54
  • @CBroe The current webpage is trying to open a site on the Internet. Do you want to allow this? Current site: http://localhost Internet site: ...q9lK/vtOdrtrt......(some long string which I believe is my Base64-encoded PDF) – xzk Jun 11 '18 at 10:03

1 Answers1

0

After looking at below 2 references, I managed to solve this problem:

https://blog.jayway.com/2017/07/13/open-pdf-downloaded-api-javascript/

Creating a Blob from a base64 string in JavaScript

My Code now:

handleDownload = (reportLoc) => {
        var path = reportLoc.item.ReportPath;
        $.get("/Api/Apps/GetFileFromServer?filePath=" + path, (response) => {
            this.setState({ base64EncodedPDF: response });
        }).then(() => {
            if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                var byteCharacters = atob(this.state.base64EncodedPDF);
                var byteNumbers = new Array(byteCharacters.length);
                for (var i = 0; i < byteCharacters.length; i++) {
                    byteNumbers[i] = byteCharacters.charCodeAt(i);
                }
                var byteArray = new Uint8Array(byteNumbers);
                var blob = new Blob([byteArray], { type: "application/octet-stream" });
                window.navigator.msSaveOrOpenBlob(blob, path.substring(path.lastIndexOf("\\") + 1));
                return;
            }
            let a = document.createElement("a");
            a.href = "data:application/octet-stream;base64," + this.state.base64EncodedPDF;
            a.download = path.substring(path.lastIndexOf("\\") + 1);
            a.click();
            });

    }
xzk
  • 827
  • 2
  • 18
  • 43