Skip to content

jshttp/content-disposition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

content-disposition

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Create an attachment Content-Disposition header

Installation

$ npm install content-disposition

API

var contentDisposition = require('content-disposition')

contentDisposition([filename])

Create an attachment Content-Disposition header value using the given file name, if supplied.

res.setHeader('Content-Disposition', contentDisposition('∫ maths.pdf'))

Examples

Send a file for download

var contentDisposition = require('content-disposition')
var destroy = require('destroy')
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 (err) {
    destroy(stream)
  })
})

Testing

$ npm test

References

License

MIT