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.

148 lines
5.1 KiB

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