You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.0 KiB

8 years ago
  1. # content-disposition
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Create and parse HTTP `Content-Disposition` header
  8. ## Installation
  9. ```sh
  10. $ npm install content-disposition
  11. ```
  12. ## API
  13. ```js
  14. var contentDisposition = require('content-disposition')
  15. ```
  16. ### contentDisposition(filename, options)
  17. Create an attachment `Content-Disposition` header value using the given file name,
  18. if supplied. The `filename` is optional and if no file name is desired, but you
  19. want to specify `options`, set `filename` to `undefined`.
  20. ```js
  21. res.setHeader('Content-Disposition', contentDisposition('∫ maths.pdf'))
  22. ```
  23. **note** HTTP headers are of the ISO-8859-1 character set. If you are writing this
  24. header through a means different from `setHeader` in Node.js, you'll want to specify
  25. the `'binary'` encoding in Node.js.
  26. #### Options
  27. `contentDisposition` accepts these properties in the options object.
  28. ##### fallback
  29. If the `filename` option is outside ISO-8859-1, then the file name is actually
  30. stored in a supplemental field for clients that support Unicode file names and
  31. a ISO-8859-1 version of the file name is automatically generated.
  32. This specifies the ISO-8859-1 file name to override the automatic generation or
  33. disables the generation all together, defaults to `true`.
  34. - A string will specify the ISO-8859-1 file name to use in place of automatic
  35. generation.
  36. - `false` will disable including a ISO-8859-1 file name and only include the
  37. Unicode version (unless the file name is already ISO-8859-1).
  38. - `true` will enable automatic generation if the file name is outside ISO-8859-1.
  39. If the `filename` option is ISO-8859-1 and this option is specified and has a
  40. different value, then the `filename` option is encoded in the extended field
  41. and this set as the fallback field, even though they are both ISO-8859-1.
  42. ##### type
  43. Specifies the disposition type, defaults to `"attachment"`. This can also be
  44. `"inline"`, or any other value (all values except inline are treated like
  45. `attachment`, but can convey additional information if both parties agree to
  46. it). The type is normalized to lower-case.
  47. ### contentDisposition.parse(string)
  48. ```js
  49. var disposition = contentDisposition.parse('attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt"');
  50. ```
  51. Parse a `Content-Disposition` header string. This automatically handles extended
  52. ("Unicode") parameters by decoding them and providing them under the standard
  53. parameter name. This will return an object with the following properties (examples
  54. are shown for the string `'attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt'`):
  55. - `type`: The disposition type (always lower case). Example: `'attachment'`
  56. - `parameters`: An object of the parameters in the disposition (name of parameter
  57. always lower case and extended versions replace non-extended versions). Example:
  58. `{filename: "€ rates.txt"}`
  59. ## Examples
  60. ### Send a file for download
  61. ```js
  62. var contentDisposition = require('content-disposition')
  63. var destroy = require('destroy')
  64. var http = require('http')
  65. var onFinished = require('on-finished')
  66. var filePath = '/path/to/public/plans.pdf'
  67. http.createServer(function onRequest(req, res) {
  68. // set headers
  69. res.setHeader('Content-Type', 'application/pdf')
  70. res.setHeader('Content-Disposition', contentDisposition(filePath))
  71. // send file
  72. var stream = fs.createReadStream(filePath)
  73. stream.pipe(res)
  74. onFinished(res, function (err) {
  75. destroy(stream)
  76. })
  77. })
  78. ```
  79. ## Testing
  80. ```sh
  81. $ npm test
  82. ```
  83. ## References
  84. - [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1][rfc-2616]
  85. - [RFC 5987: Character Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field Parameters][rfc-5987]
  86. - [RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)][rfc-6266]
  87. - [Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987][tc-2231]
  88. [rfc-2616]: https://tools.ietf.org/html/rfc2616
  89. [rfc-5987]: https://tools.ietf.org/html/rfc5987
  90. [rfc-6266]: https://tools.ietf.org/html/rfc6266
  91. [tc-2231]: http://greenbytes.de/tech/tc2231/
  92. ## License
  93. [MIT](LICENSE)
  94. [npm-image]: https://img.shields.io/npm/v/content-disposition.svg?style=flat
  95. [npm-url]: https://npmjs.org/package/content-disposition
  96. [node-version-image]: https://img.shields.io/node/v/content-disposition.svg?style=flat
  97. [node-version-url]: http://nodejs.org/download/
  98. [travis-image]: https://img.shields.io/travis/jshttp/content-disposition.svg?style=flat
  99. [travis-url]: https://travis-ci.org/jshttp/content-disposition
  100. [coveralls-image]: https://img.shields.io/coveralls/jshttp/content-disposition.svg?style=flat
  101. [coveralls-url]: https://coveralls.io/r/jshttp/content-disposition?branch=master
  102. [downloads-image]: https://img.shields.io/npm/dm/content-disposition.svg?style=flat
  103. [downloads-url]: https://npmjs.org/package/content-disposition