node/express Force browser to download file with custom name node/express Force browser to download file with custom name express express

node/express Force browser to download file with custom name


You need to add a new header to the response object to indicate the file name and do a regular download.

res.set("Content-Disposition", "attachment;filename=somefile.ext");

You could also use "inline" if instead you want the browser to try to open the file within it self, like with Chrome does with pdf files.

res.set("Content-Disposition", "inline;filename=somefile.ext");

As per @Thomas suggestion, also is a good idea to include always the right content type:

res.set("Content-Type", "application/octet-stream");


In Express 4 and later, there are 2 helper functions to change the content-type and specify attachment disposition:

res.type("application/octet-stream");res.attachment("filename.ext");

See docs for type and attachment.