174k views
2 votes
How to set content-disposition header in javascript

1 Answer

4 votes

Final answer:

To set the Content-Disposition header in JavaScript, it's typically done server-side, such as in a Node.js application. Using Express.js.

Step-by-step explanation:

To set the Content-Disposition header in JavaScript, you'll often be working in the context of a web server or when dealing with HTTP response headers. If you're working on the client-side with JavaScript, setting the Content-Disposition header is not typically done because it's a response header.

Meaning it instructs how the content should be handled by the browser once received. However, when working server-side, for example with Node.js, you might set this header using the response object in an HTTP request handler. Here is how you might set it in an Express.js application:

app.get('/download', function(req, res){
res.setHeader('Content-Disposition', 'attachment; filename=example.pdf');
res.sendFile('/path/to/example.pdf');
});
This would prompt the browser to download the file as 'example.pdf' when the /download route is accessed.

User Yeggeps
by
7.6k points