Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

formstream

node-modules800.3kMIT1.5.1TypeScript support: included

A multipart/form-data encoded stream, helper for file upload.

form, stream, multipart, form-data, upload, postfile, request

readme

formstream

NPM version CI Test coverage npm download

A multipart/form-data encoded stream, helper for file upload.

Install

npm install formstream

Quick Start

var formstream = require('formstream');
var http = require('http');

var form = formstream();

// form.file('file', filepath, filename);
form.file('file', './logo.png', 'upload-logo.png');

// other form fields
form.field('foo', 'fengmk2').field('love', 'aerdeng');

// even send file content buffer directly
// form.buffer(name, buffer, filename, mimeType)
form.buffer('file2', new Buffer('This is file2 content.'), 'foo.txt');

var options = {
  method: 'POST',
  host: 'upload.cnodejs.net',
  path: '/store',
  headers: form.headers()
};
var req = http.request(options, function (res) {
  console.log('Status: %s', res.statusCode);
  res.on('data', function (data) {
    console.log(data.toString());
  });
});

form.pipe(req);

Chaining

var fs = require('fs');
var formstream = require('formstream');

var filepath = './logo.png';
fs.stat(filepath, function (err, stat) {
  formstream()
    .field('status', 'share picture')
    .field('access_token', 'your access token')
    .file('pic', filepath, 'logo.png', stat.size)
    .pipe(process.stdout); // your request stream
});

Set min chunk buffer size

Some web servers have a limit on the number of chunks, and you can set minChunkSize to ensure the size of chunk sent to the server.

var fs = require('fs');
var FormStream = require('formstream');

var filepath = './big-file.zip';
fs.stat(filepath, function (err, stat) {
  new FormStream({
    // send >= 2MB chunk buffer size to the server
    minChunkSize: 1024 * 1024 * 2,
  }).field('status', 'share file')
    .field('access_token', 'your access token')
    .file('file', filepath, 'big-file.zip', stat.size)
    .pipe(process.stdout); // your request stream
});

API Doc

formstream([options])

Create a form instance.

Arguments

  • options.minChunkSize Number - min chunk size to emit data event

Returns

Form - form instance

FormStream#field(name, value)

Add a normal field to the form.

Arguments

  • name String - Name of field
  • value String - Value of field

Returns

Form - form instance

FormStream#file(name, filepath[, filename][, filesize])

Add a local file to be uploaded to the form.

Arguments

  • name String - Name of file field
  • filepath String - Local path of the file to be uploaded
  • filename String - Optional. Name of the file (will be the base name of filepath if empty)
  • filesize Number - Optional. Size of the file (will not generate Content-Length header if not specified)

Returns

Form - form instance

FormStream#buffer(name, buffer, filename[, contentType])

Add a buffer as a file to upload.

Arguments

  • name String - Name of field
  • buffer Buffer - The buffer to be uploaded
  • filename String - The file name that tells the remote server
  • contentType String - Optional. Content-Type (aka. MIME Type) of content (will be infered with filename if empty)

Returns

Form - form instance

FormStream#stream(name, stream, filename[, contentType][, size])

Add a readable stream as a file to upload. Event 'error' will be emitted if an error occured.

Arguments

  • name String - Name of field
  • stream stream.Readable - A readable stream to be piped
  • filename String - The file name that tells the remote server
  • contentType String - Optional. Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
  • size Number - Optional. Size of the stream (will not generate Content-Length header if not specified)

Returns

Form - form instance

FormStream#headers([headers])

Get headers for the request.

Arguments

  • headers Object - Additional headers

Example

var headers = form.headers({
  'Authorization': 'Bearer kei2akc92jmznvnkeh09sknzdk',
  'Accept': 'application/vnd.github.v3.full+json'
});

Returns

Object - Headers to be sent.

Event 'error'

Emitted if there was an error receiving data.

Event 'data'

The 'data' event emits when a Buffer was used.

See Node.js Documentation for more.

Event 'end'

Emitted when the stream has received no more 'data' events will happen.

See Node.js Documentation for more.

License

MIT

Contributors


fengmk2


xingrz


semantic-release-bot


fjc0k


mrspeiser


dead-horse


shaozj

This project follows the git-contributor spec, auto updated at Wed May 15 2024 00:34:12 GMT+0800.

changelog

Changelog

1.5.1 (2024-06-07)

Bug Fixes

  • only print top 512 bytes on buffer field debug log (#27) (9f771e8)

1.5.0 (2024-06-06)

Features

1.4.0 (2024-05-14)

Features

  • allow to set mimeType on field data (#25) (f3338f8)

1.3.1 (2023-07-28)

Bug Fixes

  • types: allow formStream to be used as a type (#24) (e1d2c00)

1.3.0 (2023-07-27)

Features

1.2.0 (2023-03-17)

Features

  • Porting from new Buffer() to Buffer.from() (#22) (f268e86)

1.2.0 / 2023-03-17

features

others

1.1.1 / 2021-04-28

fixes

others

1.1.0 / 2016-12-19

  • deps: upgrade mime to latest version (#9)

1.0.0 / 2014-11-04

  • fix(field): support stream.field(String, Number)
  • chore: fix links
  • chore: use npm scripts instead of Makefile
  • fix test case on node@0.8

0.0.8 / 2014-01-17

  • destroy source stream when formstream destroy()
  • add more test cases

0.0.7 / 2013-07-25

  • feature: always try to infer Content-Length (@xingrz)
  • doc improve

0.0.6 / 2013-07-15

  • added test cases for chaining call (@xingrz)
  • improved docs (@xingrz)
  • added chaining support (@xingrz)
  • api doc
  • fixed test causes
  • update dependencies version

0.0.5 / 2012-11-06

  • fixed stream error not catch bug

0.0.4 / 2012-11-06

  • fixed #2 support form.buffer()
  • add doc for setTotalStreamSize()

0.0.3 / 2012-11-06

  • support content-length use form.setTotalStreamSize(size)
  • update readme

0.0.2 / 2012-10-11

  • use buffer-concat support node < 0.8

0.0.1 / 2012-10-11

  • support multi streams.
  • add one stream support now.
  • Initial commit