文件文件下载无法在Firefox和IE 11中使用,但Edge和Chrome

时间:2019-01-10 12:22:30

标签: javascript

我可以通过以下简单的行来下载文件,但是在Chrome和Edge中可以正常使用,而在Firefox和IE 11中则不能。 我收到了错误(firefox)

  

myURL.createObjectURL不是函数

这是我到目前为止所拥有的:

        $.ajax({
    dataType : 'native',
    url : "api/v1/service/berichte/" + wert,
    cache : false,
    xhr : function() {// Seems like the only way to get access to the xhr
                        // object
        var xhr = new XMLHttpRequest();
        xhr.responseType = 'blob'
        return xhr;
    },
    success : function(blob) {
        // It is necessary to create a new blob object with mime-type
        // explicitly set
        // otherwise only Chrome works like it should
        var newBlob = new Blob([ blob ], {
            type : "application/pdf"
        })

        // IE doesn't allow using a blob object directly as link href
        // instead it is necessary to use msSaveOrOpenBlob
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(newBlob);
            return;
        }

        // For other browsers:
        // Create a link pointing to the ObjectURL containing the blob.


         var myURL = window.URL || window.webkitURL
         var fileURL = myURL.createObjectURL(newBlob);

        var link = document.createElement('a');
        link.href = fileURL;
        link.download = "file.pdf";
        link.click();

    }
});

0 个答案:

没有答案
相关问题