0

*** UPDATE ***


I have now discovered that the problem ONLY occurs with some (large) PDF files. Text files (some very large 2GB to test) are fine.

The file I am trying to make available is a 322Mb document created with PDFLib - and it is due to be regenerated on a daily basis. The file is not corrupted and can be opened on the computer (and printed on a digital press ~ approx 450 pages) without any problem but when trying to download through the express.js and node.js it immediately fails as described below.

Are there known issues with express.js and response.download() in terms of dynamically generated PDFs? Is there a particular byte string in the document that is causing it to fail?


I am trying to make files available for download through a Node.js/Express server.

const express = require('express')
const router = express.Router({mergeParams: true})
const fs = require('fs')
/* Get page content */
router.get('/', async function(req, res) {
const filepath = "/path/to/file/" + req.params.assetname 
console.log("Filepath: ", filepath)
res.download(filepath, (err) => {
 if (err) {
 console.log('Error downloading file:', err)
 } 
 }) 
})
module.exports = router

I have edited the original post, to represent more of what I have found out. The problem appears to manifest itself when the request/response if passed through to a routing handler.

Now the problem has a reduced impact. Files of 3mb can be downloaded but ones of 100+mb cannot.

It throws an error:

Error downloading file: Error: Request aborted
at onaborted (/.../node_modules/express/lib/response.js:1061:15)
at Immediate._onImmediate (/.../node_modules/express/lib/response.js:1103:9)
at process.processImmediate (node:internal/timers:476:21) {
 code: 'ECONNABORTED
}

I have also tried setting the Content-Type header to application/pdf but that doesn't make any difference.

asked May 13, 2025 at 15:44
1
  • Is your pdf stored as a local file ? I found this article that could help you out : medium.com/@developer.nijat/… res.dowload() does not support directly sending in-memory data like Base64 or other content Commented May 30, 2025 at 10:09

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.