https://github.com/lujjjh/content-disposition-attachment
A library to parse "attachment"s in Content-Disposition.
https://github.com/lujjjh/content-disposition-attachment
content-disposition parser
Last synced: about 1 month ago
JSON representation
A library to parse "attachment"s in Content-Disposition.
- Host: GitHub
- URL: https://github.com/lujjjh/content-disposition-attachment
- Owner: lujjjh
- License: mit
- Created: 2018-02-05T13:06:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-16T09:51:35.000Z (12 months ago)
- Last Synced: 2025-03-21T15:12:44.833Z (2 months ago)
- Topics: content-disposition, parser
- Language: JavaScript
- Homepage:
- Size: 515 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# content-disposition-attachment
[](https://www.npmjs.com/package/content-disposition-attachment)

> A library to parse "attachment"s in Content-Disposition.
## Getting started
### Install via npm
```sh
npm install content-disposition-attachment
```#### ESM
```js
import { parse } from 'content-disposition-attachment';console.log(parse('attachment; filename=foo.html'));
// => { attachment: true, filename: 'foo.html' }
```#### CommonJS
```js
const { parse } = require('content-disposition-attachment');console.log(parse('attachment; filename=foo.html'));
// => { attachment: true, filename: 'foo.html' }
```### Import from CDN
#### UMD
```html
console.log(ContentDispositionAttachment.parse('attachment; filename="foo.html"'));
// => { attachment: true, filename: 'foo.html' }```
#### ESM
```html
import { parse } from 'https://unpkg.com/[email protected]?module';
console.log(parse('attachment; filename=foo.html'));
// => { attachment: true, filename: 'foo.html' }```
## API references
### parse
> Parse a Content-Disposition.
If Content-Disposition is not "attachment", it returns `{ attachment: false }`;
otherwise, it returns `{ attachment: true }` along with parsed parameters.If errors occur when parsing parameters, a `ParseError` will be thrown.
**Examples**
```js
import { parse } from 'content-disposition-attachment';parse('inline');
// => { attachment: false }parse('attachment; filename=foo.html; foo=bar');
// => { attachment: true, filename: 'foo.html', foo: 'bar' }parse('attachment; foo');
// => ParseError: expect '='
```You can find more examples in the [unit tests](test/parse.js).