Search code examples
node.jsexpressdownloadarchive

Downloading a zip archive from Express endpoint


I have a simple issue, but it's been driving me crazy for the past 2 days. I have an archive.zip file in my node/express app which I'm trying to send via a response. My code is pretty simple, I have an endpoint /download whos only job is to do response.download("archive.zip").

However, upon downloading the file (by accessing the URL through a browser), the .zip archive is downloaded, but the files in the archive are corrupted, and I get the error Unexpected end of archive.

The actual file is a good, non-corrupted archive. I can access it on the windows filesystem without any problems. It's just when it gets transferred it gets corrupted.

I have tried setting various response headers, among which:

Content-Disposition: attachment
Content-Type: application/zip, application/content-stream
Transfer-Encoding: chunked

Nothing has worked up to this point. I have searched far and wide on google / reddit / stackoverflow and haven't come up with a solution so far.

Thank you for any or all responses.

NOTE: I'm using Express Router (require('express').Router())

NOTE2: The simple zip I'm trying to download - http://s000.tinyupload.com/?file_id=06192736420392118369


Solution

  • The problem came from bad async patterns and chaining. The operations were not executing in the correct order, thus my archive was being sent before it was finalized.