What is formstream?
The formstream npm package is used to create multipart/form-data streams, which are useful for uploading files and data via HTTP requests. It simplifies the process of constructing form data streams, making it easier to handle file uploads in Node.js applications.
What are formstream's main functionalities?
Create a form stream
This feature allows you to create a form stream, add fields and files to it, and then generate the stream for use in an HTTP request.
const formstream = require('formstream');
const form = formstream();
form.field('name', 'value');
form.file('file', '/path/to/file');
form.stream();
Add fields to the form
This feature allows you to add text fields to the form stream. Each field is added with a key-value pair.
const form = formstream();
form.field('username', 'john_doe');
form.field('email', 'john@example.com');
Add files to the form
This feature allows you to add files to the form stream. Each file is added with a key and the file path.
const form = formstream();
form.file('profile_picture', '/path/to/profile.jpg');
Generate the form stream
This feature generates the form stream after adding all the fields and files. The generated stream can then be used in an HTTP request.
const form = formstream();
form.field('username', 'john_doe');
form.file('profile_picture', '/path/to/profile.jpg');
const stream = form.stream();
Other packages similar to formstream
form-data
The form-data package is a module to create readable 'multipart/form-data' streams. It is similar to formstream in that it allows you to construct form data streams for file uploads. However, form-data is more widely used and has more extensive documentation and community support.
multiparty
The multiparty package is a module for parsing multipart/form-data, which is commonly used for file uploads. Unlike formstream, which focuses on creating form data streams, multiparty is used for parsing incoming form data streams. It is useful for handling file uploads on the server side.
busboy
The busboy package is a fast and low-level module for parsing incoming HTML form data. It is similar to multiparty in that it is used for parsing form data streams, but it is known for its performance and efficiency. It is a good choice for handling large file uploads.
formstream
A multipart/form-data encoded stream, helper for file upload.
jscoverage: 100%
Install
$ npm install formstream
Usage
var formstream = require('formstream');
var http = require('http');
var form = formstream();
form.file('file', './logo.png', 'upload-logo.png');
form.field('foo', 'fengmk2');
form.field('love', 'aerdeng');
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);
form.setTotalStreamSize(size)
: Upload file with Content-Length
If you know the ReadStream
total size and you must to set Content-Length
.
You may want to use form.setTotalStreamSize(size)
.
var formstream = require('formstream');
var http = require('http');
var fs = require('fs');
fs.stat('./logo.png', function (err, stat) {
var form = formstream();
form.file('file', './logo.png', 'upload-logo.png');
form.setTotalStreamSize(stat.size);
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);
});
License
(The MIT License)
Copyright (c) 2012 fengmk2 <fengmk2@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.