我想实现一个大文件下载(大约 10-1024 Mb)。我已经成功从 Dropbox 获取了一个文件:
operationResult = await dbx.filesDownload({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
然后我将接收到的文件与元数据捆绑在一起,并将其返回到我的 Node.js 服务器:
fileMIME = mime.lookup(operationResult.name);
const downloadResult = Object.freeze({
fileBinary: operationResult.fileBinary,
fileLength: operationResult.fileBinary.length,
fileMIME,
fileName: operationResult.name,
isSucceeded,
message
});
return downloadResult;
现在Buffer,我将从 Dropbox 获得的 a 转换为Readable流并将其通过管道传输回客户端:
res.setHeader("Content-Disposition", "attachment; filename=" + downloadResult.fileName);
res.setHeader("Content-Type", downloadResult.fileMIME);
const fileReadableStream = new Readable();
fileReadableStream.push(downloadResult.fileBinary);
fileReadableStream.push(null);
fileReadableStream.pipe(res);
到目前为止,一切都很清楚并且有效。在这里我面临第一个陷阱:我需要以某种方式在浏览器中触发下载过程。
在许多示例中,使用了一些小图像或 JSON,我们可以将其完全加载到 RAM 中,进行操作,例如转换为Base64,将其分配给a.href,并触发a.click()。但由于我的文件是 10-50 Mb,我不确定这种方法是否正确。
我已经尝试过 Fetch API:
const response = await fetch(`${host}/download?fileName=${fileName}`, {
credentials: "same-origin",
method: "POST",
mode: "cors"
});
const a = document.createElement("a");
a.href = response.text();
a.download = "MyFile.pdf";
a.click();
但我总是失败 - 没有文件错误。我还尝试使用 jQuery AJAX 和XMLHttpRequest( XHR),但仍然没有下载文件。
也许,我缺少一些东西。如何从服务器获取 10-1024 Mb 的文件?
PS 没想到像下载文件这样的小事,竟然这么复杂。
忽然笑
相关分类