npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

content-disposition

v2.0.0

Published

Create and parse Content-Disposition header

Readme

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

const contentDisposition = require('content-disposition');
const fs = require('fs');
const http = require('http');
const onFinished = require('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', contentDisposition(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