【问题标题】:Download file IE, JS下载文件 IE, JS
【发布时间】:2020-02-06 21:39:31
【问题描述】:

我有一个可以下载的事件处理程序,它在除 IE 之外的所有浏览器上都运行良好,所以我用msSaveBlob 处理它并下载文件,但是当我打开它时,它说格式不好,所以我的猜测是我没有在 blob 中使用有效数据。

$(document).on('click', '.register-file-uploader-download-file', function () {
        let $fileResultsItem = $(this).closest('.form-fields__file-results-item');
        const fileName = $fileResultsItem.find('.multiple-files-upload-item').data('filename');
        const fileUrl = $fileResultsItem.find('.multiple-files-upload-item').val();
        if (fileUrl.length > 0) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    var url = window.URL || window.webkitURL;
                    let blobFileUrl = url.createObjectURL(this.response);
                    if (window.navigator && navigator.msSaveBlob) { // For IE
                        const blobObj = new Blob([this.response], { type: 'application/octet-stream' });
                        return navigator.msSaveBlob(blobObj, fileName);
                    }
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = blobFileUrl;
                    a.download = `${fileName}`;
                    document.body.appendChild(a);
                    a.click();
                    url.revokeObjectURL(blobFileUrl);
                }
            };
            xhr.open('GET', fileUrl);
            xhr.responseType = 'blob';
            xhr.send();
        }
    });

【问题讨论】:

标签: javascript jquery download blob


【解决方案1】:

我发现了错误。代码工作正常,问题实际上是我试图下载上传到舞台服务器上的图像,当我上传和下载它时它正在工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多