msSaveOrOpenBlob work in Edge but not work in Edge WebView

Viewed 3219

Now Office Add-Ins use new Edge based WebView instead IE 11 based one. My piece of code stop working after this update. I've just try to download file here. This code works correctly if I use it in any browsers. But Office Apps like Excel or Word use WebView version of Microsoft Edge. Debug show me that function window.navigator.msSaveOrOpenBlob is undefined in this case. Please help me fix it.

I try to find any documentation on this matter but without success.

        function ClickFunc() {
        var blob = new Blob(['Some Byte Array'], { type: 'application/txt' });
        //output file name
        var fileName = "test.txt";

        //detect whether the browser is IE/Edge or another browser
        //
        //ERROR: In Edge WebView window.navigator.msSaveOrOpenBlob is undefined.  
        //
        if (window.navigator && window.navigator.msSaveOrOpenBlob) 
        {
        //To IE or Edge browser, using msSaveorOpenBlob method to download file.
            window.navigator.msSaveOrOpenBlob(blob, fileName);
        } else {
            //To another browser, create a tag to downlad file.
            //This part of code for browsers other than IE & Edge.
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            document.body.appendChild(a);
            a.setAttribute('style', 'display: none');
            a.href = url;
            a.download = fileName;
            a.click();

            window.URL.revokeObjectURL(url);
            a.remove();
        }
    }

   //I use this code in HTML to call function
   <button onclick="ClickFunc()">Click me</button>

window.navigator.msSaveOrOpenBlob is undefined when it used in WebView of Microsoft Edge.

1 Answers

Are you using Microsoft Edge WebView2? As mentioned in the article, the Edge WebView2 is using Microsoft Edge (Chromium) as the rendering engine. msSaveOrOpenBlob is exclusive in MS Edge and IE, it will be undefined in Chromium-based Edge.

Related