如何使用Blob下载Excel文件(使用fs.createReadStream进行utf-8编码?

问题描述 投票:0回答:1

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

node.js sails.js blob fs
1个回答
0
投票

阅读了大量文章后,我在这里找到了答案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也无法读取该文件,所以这是我找到的最佳解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.