首页 > 解决方案 > Excel blob 重建 = 损坏的文件

问题描述

我的服务器正在向我传输.xls内容类型的文件(下面的标题)application/vnd.ms-excel

"cache-control": "public"
"content-type": "application/vnd.ms-excel"
"last-modified": "Thu, 21 Mar 2019 09:25:53 GMT"

所以我使用 blob 技术在我的客户端上创建了文件,但里面的数据到处都是(文件打开但表损坏了)。

const file = new Blob([response.body], {type: "application/vnd.ms-excel"});
const href = URL.createObjectURL(file);

我找不到解决方案,为什么。

我尝试了几件事:

在服务器上打开文件,它可以确认问题是在传输或客户端 blob 重建

作为base64而不是二进制传输:同样的问题,以便确认问题出在blob重建部分

完整的javascript(二进制)

export(context, type, callback) {
    context.$http.get(config.api_url + config.export_path + type,
        {responseType: 'application/vnd.ms-excel'}
        )
        .then((response) => {
            if (response.status === 200) {
                const file = new Blob([response.body], {type: "application/vnd.ms-excel"});
                const href = URL.createObjectURL(file);

                // success
                callback(context, 'success', href);
            } 
        })
        .catch(function (err) {
            console.log(err)
        })

},

完整的 javascript (base64)

exportBase64(context, type, callback) {
    context.$http.get(config.api_url + config.export_path + type
    )
    .then(response => response.json())
        .then((response) => {
            if (response.file) {

                const txt = atob(response.file);
                const file = new Blob([txt], { type: "application/vnd.ms-excel" });
                const href = URL.createObjectURL(file);

                // success
                callback(context, 'success', href);
            } else {
                // error
                callback(context, 'error');
            }
        })
        .catch(function (err) {
            console.log(err)
        })

}

标签: javascriptexcel

解决方案


推荐阅读