Tuesday 23 May 2017

Node.js file extensions inside gzip compression

I'm want to let the client download gzip compressed file on specific url. I got it almost working - downloading {fileName}.gz works, but when it is decompressed it contains plain file name, without any extension. My files are images, so I want .png or .jpg for example. How should I do that? So far I got this:

File is stored on the server, with unique name, generated by shortid. On GET to the specific url I find my product in the database, get the name and set it as filename like so:

res.setHeader("Content-Disposition", "attachment; filename=test.gz");
res.writeHead(200, {"Content-Type": `image/png`});

then the compression:

let gzip = zlib.createGzip();
fs.createReadStream(filePath)
  .pipe(gzip)
  .pipe(res);

My problem is that setHeader only sets the name of the original file, which is the compressed .gz file. How can I set the name of the file to be decompressed to "my-image.png`?



via Alex

No comments:

Post a Comment