linkElement.dispatchevent 对象不支持此操作 IE



我的代码就像波纹管:

         var file = new Blob([response.data], { type: 'application/pdf' });
            if (file.size != 0) {
                var objectUrl = URL.createObjectURL(file);
                var linkElement = document.createElement('a');
                linkElement.setAttribute('href', objectUrl);
                linkElement.setAttribute("download", fileName);
                var clickEvent;
                //This is true only for IE,firefox
                if (document.createEvent) {
                    // To create a mouse event , first we need to create an event and then initialize it.
                    clickEvent = document.createEvent("MouseEvent");
                    clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                }
                else {
                    clickEvent = new MouseEvent('click', {
                        'view': window,
                        'bubbles': true,
                        'cancelable': true
                    });
                }                   
                linkElement.dispatchEvent(clickEvent);

此代码在chrome和mozila中运行良好,仅在IE 10或更高版本上出现问题。

谁能帮我让它工作?

谢谢。

此方法并不适合所有浏览器。因此,您可以简单地使用Filesaver.js https://github.com/eligrey/FileSaver.js

在页面中包含 Filesaver.js,并使用此文件中的 saveAs 方法作为波纹管。

var file = new Blob([response.data], { type: 'application/pdf' });
if (file.size != 0) 
{
  $window.saveAs(file, fileName);                   
}

最新更新