looking for some solutions? You are welcome.

SOLVED: In Express.js, why does my downloaded file have a different size to the server file size?

Hoa:

My express.js code is very simple:

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

and

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

My client code is also straightforward:

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

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

When downloaded from the browser, the file is corrupt and I can't open it. The original /tmp/my-file.zip is 119506 bytes. Strangely, the downloaded export.zip is 216980 bytes. I'm running everything locally at the moment so there are not operating system differences that would explain this.

Why are my file sizes different (leading to a corrupt .zip file) and how do I fix this?

Edit - these are the browser headers:

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"



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent