content-disposition
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
safe-buffer | 5.2.1 | 9.74 kB | MIT | prod |
Content-Dispostion is an npm package utilized for creating and parsing HTTP 'Content-Disposition' headers. It is particularly useful when you need to communicate information about the file being sent or received. This can include specifications such as whether the file is to be displayed inline in the browser, or presented as a file download.
To use the Content-Disposition package, you first need to install it via npm using the command npm install content-disposition
. Once installed, you can require this package in your JavaScript file with var contentDisposition = require('content-disposition')
.
You can create a 'Content-Disposition' header using the contentDisposition(filename, options)
function, where filename is the name of the file you're working with, and options is an optional parameter specifying additional details. An example usage can be res.setHeader('Content-Disposition', contentDisposition('β« maths.pdf'))
.
If you want to parse a 'Content-Disposition' header string, you can use the contentDisposition.parse(string)
function. This function takes the header string as an input and automatically handles the decoding of any extended ("Unicode") parameters, returning them under the standard parameter name.
Here is an example of sending a file for download:
var contentDisposition = require('content-disposition')
var destroy = require('destroy')
var fs = require('fs')
var http = require('http')
var onFinished = require('on-finished')
var filePath = '/path/to/public/plans.pdf'
http.createServer(function onRequest (req, res) {
// set headers
res.setHeader('Content-Type', 'application/pdf')
res.setHeader('Content-Disposition', contentDisposition(filePath))
// send file
var stream = fs.createReadStream(filePath)
stream.pipe(res)
onFinished(res, function () {
destroy(stream)
})
})
The 'destroy' and 'on-finished' modules are used to handle cleanup after the file is sent.
The official documentation for the Content-Disposition package is contained within the README file in the GitHub repository (https://github.com/jshttp/content-disposition). It provides a comprehensive overview of the package's API, options, and examples of how you can use it. For more specific details or technicalities related to 'Content-Disposition' header, refer to the RFCs linked in the "References" section of the package's README.