FileSaver 创建一个内容与原始文件不同的文件

FileSaver creates a file with different content than the original

我正在尝试使用 Blob 从通过服务器调用传递的数据创建一个文件,以 base64 编码。原始文件是一个 Excel 电子表格,长度为 5,507 字节。 base64 编码(带换行符)为 7,441 字节。使用命令行 base64 工具,我能够从 base64 版本构建原始副本的精确副本:

% base64 file.xlsx > file.enc
% base64 -d file.enc > copy.xlsx
% cmp file.xlsx copy.xlsx
% file --mime-type copy.xlsx
copy.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

在 Typescript 中,我正在做:

import * as dld from 'file-saver';

const contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
const data = atob(content)
const contentBlob = new Blob([data], { type: contentType });

dld.saveAs(contentBlob);

我已验证二进制数据(上面的变量 data)至少以与原始文件的二进制内容相同的字节开始。它创建的文件长 7,729 字节,MIME 类型为 application/zip 而不是预期的 5,507 字节。我在这里错过了什么?为了创建文件的客户端副本,我该怎么做?

使用文件保护程序时,我们必须从 Uint8Array

初始化 blob

const pdfFileName = 'someName' const fileDownload: Blob = new Blob([new Uint8Array(res.body)], { type: contentType }); fileSaver.saveAs(fileDownload, pdfFileName);