54179d3ddeeb49393df168db1867d19054c44b60e1da5ff5555d26a8f0ba9cc5f3d5dadce76ebcf0110924c440a10e01969ff701076a38a2ff8673db6325d9 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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][github-actions-ci-image]][github-actions-ci-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 fs = require('fs')
  65. var http = require('http')
  66. var onFinished = require('on-finished')
  67. var filePath = '/path/to/public/plans.pdf'
  68. http.createServer(function onRequest (req, res) {
  69. // set headers
  70. res.setHeader('Content-Type', 'application/pdf')
  71. res.setHeader('Content-Disposition', contentDisposition(filePath))
  72. // send file
  73. var stream = fs.createReadStream(filePath)
  74. stream.pipe(res)
  75. onFinished(res, function () {
  76. destroy(stream)
  77. })
  78. })
  79. ```
  80. ## Testing
  81. ```sh
  82. $ npm test
  83. ```
  84. ## References
  85. - [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1][rfc-2616]
  86. - [RFC 5987: Character Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field Parameters][rfc-5987]
  87. - [RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)][rfc-6266]
  88. - [Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987][tc-2231]
  89. [rfc-2616]: https://tools.ietf.org/html/rfc2616
  90. [rfc-5987]: https://tools.ietf.org/html/rfc5987
  91. [rfc-6266]: https://tools.ietf.org/html/rfc6266
  92. [tc-2231]: http://greenbytes.de/tech/tc2231/
  93. ## License
  94. [MIT](LICENSE)
  95. [npm-image]: https://img.shields.io/npm/v/content-disposition.svg
  96. [npm-url]: https://npmjs.org/package/content-disposition
  97. [node-version-image]: https://img.shields.io/node/v/content-disposition.svg
  98. [node-version-url]: https://nodejs.org/en/download
  99. [coveralls-image]: https://img.shields.io/coveralls/jshttp/content-disposition.svg
  100. [coveralls-url]: https://coveralls.io/r/jshttp/content-disposition?branch=master
  101. [downloads-image]: https://img.shields.io/npm/dm/content-disposition.svg
  102. [downloads-url]: https://npmjs.org/package/content-disposition
  103. [github-actions-ci-image]: https://img.shields.io/github/workflow/status/jshttp/content-disposition/ci/master?label=ci
  104. [github-actions-ci-url]: https://github.com/jshttp/content-disposition?query=workflow%3Aci