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

Javascript 在Express.js中,为什么我下载的文件与服务器文件大小不同?,javascript,node.js,express,Javascript,Node.js,Express,我的express.js代码非常简单: app.get("/download", download); 及 我的客户端代码也很简单: import axios from "axios"; const fileDownload = require("js-file-download"); axios.get("/download").then(response => { fileDownload(response.data, "export.zip"); }); 从浏览器下载时,文

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

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

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

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

axios.get("/download").then(response => {
  fileDownload(response.data, "export.zip");
});
从浏览器下载时,文件已损坏,我无法打开它。原始的
/tmp/my file.zip
119506
字节。奇怪的是,下载的
export.zip
216980
字节。我现在在本地运行所有的东西,所以没有操作系统的差异可以解释这一点

为什么我的文件大小不同(导致.zip文件损坏),如何修复

编辑-这些是浏览器标题:

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"

我找到了一个解决办法。因为responseType默认值是json

//
responseType
指示服务器将使用的数据类型 响应//选项为“arraybuffer”、“blob”、“document”, 'json'、'text'、'stream'响应类型:'json',//默认值


希望它能起作用:)

您得到的是什么类型的回复?它是json吗?我已经编辑了我的问题,以包括浏览器标题。它应该是一个基于内容类型的zip文件。如果您只需使用浏览器导航到
/下载
,会发生什么情况?@felixmosh这是一个很好的测试,效果很好。我猜axios或js文件下载会改变文件。那么为什么要通过ajax使用它呢?只需在其上放置一个带有
download
属性的链接,即可下载文件:]
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"
axios.get('/download',{ responseType:'arraybuffer'})
.then(function (response) {
    console.log('########### HEADERS: ', response.headers);
    console.log('########### AXIOS: ', response.data.length);
  fileDownload(response.data, "export.zip");
});