content-disposition rewrite with browser and Node.js support
npm install content-disposition-header[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][github-actions-image]][github-actions-url]
[![Test Coverage][codecov-image]][codecov-url]
> content-disposition rewrite with browser and Node.js support.
Create and parse HTTP Content-Disposition header
``sh`
$ npm install content-disposition-header
or
`sh`
$ yarn add content-disposition-header
`js`
var { create, parse } = require("content-disposition-header");
or
`js`
import { create, parse } from "content-disposition-header";
Create an attachment Content-Disposition header value using the given file name,filename
if supplied. The is optional and if no file name is desired, but youoptions
want to specify , set filename to undefined.
`js`
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'binary'
the 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 thetrue
Unicode version (unless the file name is already ISO-8859-1).
- 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 afilename
different value, then the 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 likeattachment, but can convey additional information if both parties agree to
it). The type is normalized to lower-case.
`js`
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'attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt'
("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 ):
- type: The disposition type (always lower case). Example: 'attachment'
- parameters: An object of the parameters in the disposition (name of parameter{filename: "€ rates.txt"}
always lower case and extended versions replace non-extended versions). Example:
`js
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);
});
});
`
`sh``
$ npm test
- [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1][rfc-2616]
- [RFC 5987: Character Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field Parameters][rfc-5987]
- [RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)][rfc-6266]
- [Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987][tc-2231]
[rfc-2616]: https://tools.ietf.org/html/rfc2616
[rfc-5987]: https://tools.ietf.org/html/rfc5987
[rfc-6266]: https://tools.ietf.org/html/rfc6266
[tc-2231]: http://greenbytes.de/tech/tc2231/
[npm-image]: https://img.shields.io/npm/v/content-disposition-header.svg
[npm-url]: https://npmjs.org/package/content-disposition-header
[github-actions-image]: https://img.shields.io/github/workflow/status/Methuselah96/content-disposition-header/CI.svg
[github-actions-url]: https://github.com/Methuselah96/content-disposition-header/actions/workflows/CI.yml
[codecov-image]: https://img.shields.io/codecov/c/github/Methuselah96/content-disposition-header.svg
[codecov-url]: https://app.codecov.io/gh/Methuselah96/content-disposition-header
[downloads-image]: https://img.shields.io/npm/dm/content-disposition-header.svg
[downloads-url]: https://npmjs.org/package/content-disposition-header