gpt4 book ai didi

javascript - 在 Express.js 中,为什么我下载的文件大小与服务器文件大小不同?

转载 作者:搜寻专家 更新时间:2023-10-31 23:56:42 31 4
gpt4 key购买 nike

我的 express.js 代码非常简单:

app.get("/download", download);

export let download = async (req: Request, res: Response) => {
const file = "/tmp/my-file.zip";
res.download(file);
}

我的客户端代码也很简单:

import axios from "axios";
const fileDownload = require("js-file-download");

axios.get("/download").then(response => {
fileDownload(response.data, "export.zip");
});

从浏览器下载时,文件已损坏,无法打开。原来的/tmp/my-file.zip119506字节。奇怪的是,下载的 export.zip216980 字节。我目前在本地运行所有内容,因此没有操作系统差异可以解释这一点。

为什么我的文件大小不同(导致 .zip 文件损坏),我该如何解决?

编辑 - 这些是浏览器 header :

accept-ranges: "bytes"
cache-control: "no-store, no-cache, must-revalidate, proxy-revalidate"
connection: "keep-alive"
content-disposition: "attachment; filename="my-file.zip""
content-length: "119506"
content-type: "application/zip"
date: "Thu, 14 Mar 2019 06:04:28 GMT"
etag: "W/"1d2d2-1697acd3f53""
expires: "0"
last-modified: "Thu, 14 Mar 2019 06:04:25 GMT"
pragma: "no-cache"
referrer-policy: "no-referrer"
surrogate-control: "no-store"
x-content-type-options: "nosniff"
x-frame-options: "SAMEORIGIN"
x-xss-protection: "1; mode=block"

最佳答案

我在这个 post 找到了解决方案.因为axios responseType 默认为json

// responseType indicates the type of data that the server will respond with // options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream' responseType: 'json', // default

axios.get('/download',{ responseType:'arraybuffer'})
.then(function (response) {
console.log('########### HEADERS: ', response.headers);
console.log('########### AXIOS: ', response.data.length);
fileDownload(response.data, "export.zip");
});

希望它有用:)

关于javascript - 在 Express.js 中,为什么我下载的文件大小与服务器文件大小不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155715/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com