jQuery函数下载blob在IO中不起作用

时间:2019-11-23 20:03:57

标签: jquery ios

我有以下代码,这些代码已经在不同浏览器的Windows上进行了测试,并且可以分别正常工作,但是没有问题,但是我已经尝试在ipad和iphone上使用它,但它们不起作用(Safari和Google chrome)。

在代码中,我发送了通过ajax(Blob)接收的参数,以便生成文件并下载文档.pdf,但是我无法在IO中使用它 我在做什么错了?

function descargar(response, status, xhr, nombre){
        var filename = 'CV_'+nombre+'.pdf';                  
        var disposition = xhr.getResponseHeader('Content-Disposition');

        if (disposition) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches !== null && matches[1]) filename = matches[1].replace(/['"]/g, '');
        } 
            var linkelem = document.createElement('a');
            try {
                var blob = new Blob([response], { type: 'application/octet-stream' });                        
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    //   IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                    window.navigator.msSaveBlob(blob, filename);
                } 
                else{
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    if (filename) { 
                        // use HTML5 a[download] attribute to specify filename
                        var a = document.createElement("a");
                        // safari doesn't support this yet
                        if (typeof a.download === 'undefined') {
                            window.location = downloadUrl;
                        } else {
                            a.href = downloadUrl;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.target = "_blank";
                            a.click();
                        }
                    } else{
                        window.location = downloadUrl;
                    }
                }   

            } catch (ex) {
                console.log(ex);
            }
}

0 个答案:

没有答案