• Stars
    star
    220
  • Rank 180,422 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create and parse HTTP Content-Disposition header

content-disposition

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

Create and parse HTTP Content-Disposition header

Installation

$ npm install content-disposition

API

var contentDisposition = require('content-disposition')

contentDisposition(filename, options)

Create an attachment Content-Disposition header value using the given file name, if supplied. The filename is optional and if no file name is desired, but you want to specify options, set filename to undefined.

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

note HTTP headers are of the ISO-8859-1 character set. If you are writing this header through a means different from setHeader in Node.js, you'll want to specify the 'binary' encoding in Node.js.

Options

contentDisposition accepts these properties in the options object.

fallback

If the filename option is outside ISO-8859-1, then the file name is actually stored in a supplemental field for clients that support Unicode file names and a ISO-8859-1 version of the file name is automatically generated.

This specifies the ISO-8859-1 file name to override the automatic generation or disables the generation all together, defaults to true.

  • A string will specify the ISO-8859-1 file name to use in place of automatic generation.
  • false will disable including a ISO-8859-1 file name and only include the Unicode version (unless the file name is already ISO-8859-1).
  • true will enable automatic generation if the file name is outside ISO-8859-1.

If the filename option is ISO-8859-1 and this option is specified and has a different value, then the filename option is encoded in the extended field and this set as the fallback field, even though they are both ISO-8859-1.

type

Specifies the disposition type, defaults to "attachment". This can also be "inline", or any other value (all values except inline are treated like attachment, but can convey additional information if both parties agree to it). The type is normalized to lower-case.

contentDisposition.parse(string)

var disposition = contentDisposition.parse('attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt')

Parse a Content-Disposition header string. This automatically handles extended ("Unicode") parameters by decoding them and providing them under the standard parameter name. This will return an object with the following properties (examples are shown for the string 'attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt'):

  • type: The disposition type (always lower case). Example: 'attachment'

  • parameters: An object of the parameters in the disposition (name of parameter always lower case and extended versions replace non-extended versions). Example: {filename: "€ rates.txt"}

Examples

Send 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)
  })
})

Testing

$ npm test

References

License

MIT

More Repositories

1

http-errors

Create HTTP Errors
JavaScript
1,489
star
2

mime-types

The ultimate javascript content-type utility.
JavaScript
1,311
star
3

cookie

HTTP server cookie parsing and serialization
JavaScript
1,303
star
4

mime-db

Media Type Database
JavaScript
1,083
star
5

basic-auth

Generic basic auth Authorization header field parser
JavaScript
704
star
6

on-finished

Execute a callback when a request closes, finishes, or errors
JavaScript
389
star
7

negotiator

An HTTP content negotiator for Node.js
JavaScript
307
star
8

statuses

HTTP status utility
JavaScript
269
star
9

accepts

Higher-level content negotiation
JavaScript
252
star
10

etag

Create simple HTTP ETags
JavaScript
251
star
11

type-is

Infer the content-type of a request.
JavaScript
226
star
12

methods

HTTP verbs that node supports
JavaScript
177
star
13

fresh

HTTP request freshness testing
JavaScript
159
star
14

on-headers

Execute a listener when a response is about to write headers.
JavaScript
154
star
15

http-assert

assert with status codes
JavaScript
151
star
16

proxy-addr

Determine address of proxied request
JavaScript
132
star
17

content-type

Create and parse HTTP Content-Type header
JavaScript
130
star
18

style-guide

jshttp style guide
JavaScript
108
star
19

compressible

Compressible Content-Type / mime checking.
JavaScript
98
star
20

range-parser

Range header field parser
JavaScript
90
star
21

vary

Manipulate the HTTP Vary header
JavaScript
61
star
22

forwarded

Parse HTTP X-Forwarded-For header
JavaScript
56
star
23

media-typer

Simple RFC 6838 media type parser and formatter
JavaScript
54
star
24

jshttp.github.io

HTML
47
star
25

spdy-push

SPDY Push helper - will by replaced by http-push
JavaScript
29
star
26

http-push

14
star
27

http-utils

Low-level HTTP parsing/formatting utilities
JavaScript
14
star
28

.github

1
star