Git Product home page Git Product logo

formstream's Introduction

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.

formstream's People

Contributors

dead-horse avatar fengmk2 avatar fjc0k avatar mrspeiser avatar semantic-release-bot avatar shaozj avatar xingrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

formstream's Issues

formstream的package.json文件依赖的"mime": "^1.3.4",

安装依赖会安装2.0版本,因为node-mime代码方法名改了
Version 2 is a breaking change from 1.x as the semver implies. Specifically:

lookup() renamed to getType()
extension() renamed to getExtension()
charset() and load() methods have been removed
If you prefer the legacy version of this module please npm install mime@^1. Version 1 docs may be found here.
这样会引发报错
nodejs.TypeError: mime.lookup is not a function
请更新下指定版本

创建的maltipart有问题

当批量上传时,这里有很多无关的信息被创建了,导致请求400
单个上传没有这个问题
image
image

path.replace is not a function

代码:

const formstream = require('formstream')
const fs = require('fs')
const path = require('path')
const filepath = path.join(__dirname, '1.jpg')
const file = fs.createReadStream(filepath)

const stream = formstream()
stream.file('uri', file)
stream.field('hello', 'world')
console.log(stream)

报错:

  var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase();
                 ^

TypeError: path.replace is not a function
 at Mime.lookup (/project/node_modules/mime/mime.js:70:18)
    at FormStream.file (/project/node_modules/formstream/lib/formstream.js:113:23)
    at Object.<anonymous> (/project/form-data.js:35:8)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.