content-disposition

中文文档
3.0.0发布于 2026/08/14MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6.4亿

Create and parse Content-Disposition header

原始使用文档
文档目录11 节
  1. Installation
  2. API
  3. create(filename, options)
  4. parse(string, options)
  5. format(obj, options)
  6. Examples
  7. Send a file for download
  8. Local demo
  9. Testing
  10. References
  11. License

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

import { create, parse, format } from 'content-disposition';

create(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', create('∫ 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 US-ASCII, then the file name is actually stored in a supplemental field for clients that support Unicode file names and a US-ASCII version of the file name is automatically generated.

This specifies the US-ASCII file name to override the automatic generation or disables the generation all together, defaults to true.

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

If the filename option is US-ASCII 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 US-ASCII.

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).

parse(string, options)

const disposition = 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:

  • 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"}

Options

multipart

Parse parameters using browser multipart/form-data behavior.

parse('form-data; name="file"; filename="the %22plans%22.pdf"', {
  multipart: true,
});
extended

Parse RFC 5987 extended header parameters automatically when decoding parameters, defaults to true.

format(obj, options)

const disposition = format({
  type: 'attachment',
  parameters: {
    filename: '€ rates.txt',
  },
});

Formats an object to a Content-Disposition header string. This automatically handles extended ("Unicode") parameters and returns a string. Example: 'attachment; filename*=UTF-8''%E2%82%AC%20rates.txt'

Options

multipart

Format parameters using browser multipart/form-data behavior. This quotes parameter values, escapes " as %22, and writes Unicode values directly instead of using extended parameters.

format(
  {
    type: 'form-data',
    parameters: { name: 'file', filename: '€ rates.txt' },
  },
  { multipart: true },
);
extended

Encode Unicode parameter values using RFC 5987 extended header parameters, e.g. filename*=, defaults to true.

Examples

Send a file for download

import fs from 'node:fs';
import http from 'node:http';
import { create } from 'content-disposition';
import onFinished from 'on-finished';

const filePath = '/path/to/public/plans.pdf';

http.createServer(function onRequest(req, res) {
  // set headers
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', create(filePath));

  // send file
  const stream = fs.createReadStream(filePath);
  stream.pipe(res);
  onFinished(res, function () {
    stream.destroy();
  });
});

Local demo

Run the upload inspector locally:

npm run demo

Then open http://127.0.0.1:3000 in your browser. The demo lets you upload files, inspect the multipart upload part headers sent by the browser, and compare them with the download Content-Disposition header generated by this package.

Testing

$ npm test

References

License

MIT

版本与发布记录

content-disposition 共发布 18 个版本,首次发布于 2014/09/19;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/08/142 个月前
2.0.12026/05/285 个月前
2.0.02026/05/125 个月前
1.1.02026/04/086 个月前
1.0.12025/11/1811 个月前
1.0.0next2024/09/012 年前

关于 content-disposition 的常见问题

content-disposition 是什么?

Create and parse Content-Disposition header

如何安装 content-disposition?

在项目目录运行 npm install content-disposition,或使用 pnpm add / yarn add / bun add。

content-disposition 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

content-disposition 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

content-disposition 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

content-disposition 可以商用吗?

content-disposition 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

content-disposition 的下载量有多少?

根据 npm 官方下载统计,content-disposition 在 2026/10/05 – 2026/11/03 这 30 天内被下载 641,388,857 次,日均约 21,379,629 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

content-disposition 还在维护吗?

最新版本 3.0.0 发布于 2026/08/14(2 个月前);至今共发布 18 个版本,其中过去 12 个月发布了 5 个;首次发布于 2014/09/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。