如何使用 Blob 下载 excel 文件(utf-8 编码 fs.createReadStream)?

How to download a excel file (utf-8 encoding with fs.createReadStream) using Blob?

我用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 文件。

看了很多帖子,在这里找到了答案

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

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 读取,所以这是我找到的最佳解决方案。