Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript | IE11下载base64数据_Javascript_Jquery - Fatal编程技术网

使用javascript | IE11下载base64数据

使用javascript | IE11下载base64数据,javascript,jquery,Javascript,Jquery,我正在尝试使用JavaScript中的“window.location.href”下载base64数据。它在Chrome中运行良好,但在IE11中无法使用相同的代码 您能告诉我修复方法或解决方法吗 代码如下: Javascript: function DownloadPdf() { window.location.href = "data:application/pdf;base64,JVBERi0xLjMNJeLjz9MNCj........Pg1zdGFydHhyZWYNMTczDSUlRU

我正在尝试使用JavaScript中的“window.location.href”下载base64数据。它在Chrome中运行良好,但在IE11中无法使用相同的代码

您能告诉我修复方法或解决方法吗

代码如下: Javascript:

function DownloadPdf() {
window.location.href = "data:application/pdf;base64,JVBERi0xLjMNJeLjz9MNCj........Pg1zdGFydHhyZWYNMTczDSUlRU9GDQ=="
}

HTML:

注:

我正在开发一个离线网站,在这里我以base64字符串格式将文件存储在浏览器localStorage中,该格式未连接到服务器。我没有任何物理文件

我发现javascript在本例中可能对您有用,它是为下载指定MIME类型的base64内容而开发的。 此外 请看哪一个解释了如何下载base64编码的内容

function fnExport(base64encodedstring) {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    // If Internet Explorer:
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        txtArea1.document.open("content type", "replace");
        txtArea1.document.write(base64encodedstring);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, reportname + ".extension");
        console.log(sa);
    }
    else { //other browser not tested on IE 11
        sa = window.open('data:content-type;base64,' +base64encodedstring);
    }
    return (sa);
}

下面是我在所有浏览器中的工作

var blob = new Blob([tdData], { type: 'text/csv' });
if (window.navigator.msSaveBlob) { // // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
    window.navigator.msSaveOrOpenBlob(blob, 'exportData' + new Date().toDateString() + '.csv');
}
else {
    var a = window.document.createElement("a");
    a.href = window.URL.createObjectURL(blob, { type: "text/plain" });
    a.download = "exportData" + new Date().toDateString() + ".csv";
    document.body.appendChild(a);
    a.click();  // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
    document.body.removeChild(a);
}

按照以下步骤将Word文档从.NETWebAPI下载到ajax

  • 将文件转换为base64格式(在C#中这只是两行代码)
  • 将base64字符串返回到前端
  • 使用下面的函数将base64字符串转换为blob

    base64toBlob = function (base64Data, contentType) {
        contentType = contentType || '';
        var sliceSize = 1024;
        var byteCharacters = atob(base64Data);
        //var byteCharacters = decodeURIComponent(escape(window.atob(base64Data)))
        var bytesLength = byteCharacters.length;
        var slicesCount = Math.ceil(bytesLength / sliceSize);
        var byteArrays = new Array(slicesCount);
    
        for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
            var begin = sliceIndex * sliceSize;
            var end = Math.min(begin + sliceSize, bytesLength);
    
            var bytes = new Array(end - begin);
            for (var offset = begin, i = 0 ; offset < end; ++i, ++offset) {
                bytes[i] = byteCharacters[offset].charCodeAt(0);
            }
            byteArrays[sliceIndex] = new Uint8Array(bytes);
        }
        return new Blob(byteArrays, { type: contentType });
    }
    
    base64toBlob=函数(base64Data,contentType){
    contentType=contentType | |“”;
    var-sliceSize=1024;
    var byteCharacters=atob(base64Data);
    //var byteCharacters=decodeURIComponent(escape(window.atob(base64Data)))
    var bytesLength=byteCharacters.length;
    var slicescont=Math.ceil(字节长度/切片大小);
    var byteArray=新阵列(切片器);
    对于(var sliceIndex=0;sliceIndex
  • 将blob渲染为文件。这在Chrome和IE中的处理方式略有不同。对于Chrome,我们需要使用链接下载选项

  • 请参考IE 11的此链接:

    base64toBlob = function (base64Data, contentType) {
        contentType = contentType || '';
        var sliceSize = 1024;
        var byteCharacters = atob(base64Data);
        //var byteCharacters = decodeURIComponent(escape(window.atob(base64Data)))
        var bytesLength = byteCharacters.length;
        var slicesCount = Math.ceil(bytesLength / sliceSize);
        var byteArrays = new Array(slicesCount);
    
        for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
            var begin = sliceIndex * sliceSize;
            var end = Math.min(begin + sliceSize, bytesLength);
    
            var bytes = new Array(end - begin);
            for (var offset = begin, i = 0 ; offset < end; ++i, ++offset) {
                bytes[i] = byteCharacters[offset].charCodeAt(0);
            }
            byteArrays[sliceIndex] = new Uint8Array(bytes);
        }
        return new Blob(byteArrays, { type: contentType });
    }