【问题标题】:How to download a excel file (utf-8 encoding with fs.createReadStream) using Blob?如何使用 Blob 下载 excel 文件(使用 fs.createReadStream 的 utf-8 编码)?
【发布时间】:2020-03-01 14:43:16
【问题描述】:

我用excel4node成功创建了一个excel文件并保存在服务器中,然后我使用读取流将文件发送到客户端:

            res.set({
                'Content-Type': 'application/vnd.openxmlformats',
                'Content-Disposition': 'attachment; filename='+filename
            });

            // This line opens the file as a readable stream
            var readStream = fs.createReadStream(path+filename, {encoding: 'utf8'});

            // This will wait until we know the readable stream is actually valid before piping
            readStream.on('open', function () {
                // This just pipes the read stream to the response object (which goes to the client)
                readStream.pipe(res);
            });
            // This catches any errors that happen while creating the readable stream (usually invalid names)
            readStream.on('error', function(err) {
            res.end(err);
            });

之后我在浏览器中获取数据并使用 Blob 下载:

        var blob = new Blob([data], { type: 'application/vnd.openxmlformats' });
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = "File.xlsx";

        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);

当我尝试打开文件时,我收到以下消息:

"Excel found unreadable content in 'File.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes"

如果我单击“是”,Excel 会说文件已损坏且无法恢复。

我想知道如何从服务器检索 excel 文件。

【问题讨论】:

  • 文件有多大?一些浏览器对数据 URL 的长度有限制。您是否也尝试过手动查看文件以查看其外观?
  • 文件很小,不到10KiB,文件还可以,我用excel打开了后台创建的文件
  • 不,你有没有用文本编辑器或十六进制编辑器查看从前端下载的文件?
  • 是的,但这是一堆难以辨认的字符

标签: node.js sails.js blob fs


【解决方案1】:

看了很多帖子,在这里How to save .xlsx data to file as a blob找到了答案

我必须在后端使用'base64-stream'和'excel4node'.write函数将流编码为base64

wb.write(path+filename, function(err) {
                if (err) {
                    sails.log(err);
                } else {
                    // This line opens the file as a readable stream
                    var readStream = fs.createReadStream(path+filename);

                    // This will wait until we know the readable stream is actually valid before piping
                    readStream.on('open', function () {
                        // This just pipes the read stream to the response object (which goes to the client)
                        readStream.pipe(new Base64Encode()).pipe(res);
                    });
                    // This catches any errors that happen while creating the readable stream (usually invalid names)
                    readStream.on('error', function(err) {
                        res.end(err);
                    });
                }
            });

之后我在浏览器中收到如下数据:

.done(function(data){

      function s2ab(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
      }

        var blob = new Blob([s2ab(atob(data))], { type: 'application/vnd.openxmlformats' });
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = "ExcelFile.xlsx";

        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);
    })

问题出在编码上,即使你使用 'utf16le' 文件也无法被 excel 读取,所以这是我找到的最佳解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 2017-09-04
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多