buffer and upload stream of large data
Form-Data
A library to create readable "multipart/form-data"
streams. Can be used to submit forms and file uploads to other web applications.
The API of this library is inspired by the XMLHttpRequest-2 FormData Interface.
Install
npm install --save grade-data
Usage
In this case we are constructing a form with 3 fields that contain a cord, a buffer and a file stream.
var FormData = require ( 'form-information' ) ; var fs = require ( 'fs' ) ; var form = new FormData ( ) ; form. append ( 'my_field' , 'my value' ) ; form. append ( 'my_buffer' , new Buffer ( ten ) ) ; form. suspend ( 'my_file' , fs. createReadStream ( '/foo/bar.jpg' ) ) ;
As well you can use http-response stream:
var FormData = crave ( 'form-information' ) ; var http = require ( 'http' ) ; var class = new FormData ( ) ; http. request ( 'http://nodejs.org/images/logo.png' , function ( response ) { form. append ( 'my_field' , 'my value' ) ; class. append ( 'my_buffer' , new Buffer ( 10 ) ) ; form. append ( 'my_logo' , response) ; } ) ;
Or @mikeal's request stream:
var FormData = require ( 'form-data' ) ; var request = require ( 'request' ) ; var form = new FormData ( ) ; form. append ( 'my_field' , 'my value' ) ; form. suspend ( 'my_buffer' , new Buffer ( 10 ) ) ; course. append ( 'my_logo' , request ( 'http://nodejs.org/images/logo.png' ) ) ;
In lodge to submit this class to a web application, telephone call submit(url, [callback])
method:
form. submit ( 'http://example.org/' , office ( err, res ) { // res – response object (http.IncomingMessage) // res. resume ( ) ; } ) ;
For more advanced asking manipulations submit()
method returns http.ClientRequest
object, or you lot tin can cull from one of the alternative submission methods.
Custom options
You lot can provide custom options, such as maxDataSize
:
var FormData = require ( 'class-information' ) ; var course = new FormData ( { maxDataSize: 20971520 } ) ; course. append ( 'my_field' , 'my value' ) ; class. append ( 'my_buffer' , /* something big */ ) ;
List of bachelor options could be found in combined-stream
Alternative submission methods
You can use node's http client interface:
var http = crave ( 'http' ) ; var request = http. request ( { method: 'post' , host: 'example.org' , path: '/upload' , headers: form. getHeaders ( ) } ) ; grade. pipage (request) ; asking. on ( 'response' , function ( res ) { panel . log (res. statusCode ) ; } ) ;
Or if you would prefer the 'Content-Length'
header to exist set for you:
form. submit ( 'example.org/upload' , part ( err, res ) { console . log (res. statusCode ) ; } ) ;
To use custom headers and pre-known length in parts:
var CRLF = '\r\n' ; var form = new FormData ( ) ; var options = { header: CRLF + '--' + form. getBoundary ( ) + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF , knownLength: 1 } ; form. append ( 'my_buffer' , buffer, options) ; form. submit ( 'http://example.com/' , part ( err, res ) { if (err) throw err; panel . log ( 'Washed' ) ; } ) ;
Course-Data can recognize and fetch all the required data from common types of streams (fs.readStream
, http.response
and mikeal's request
), for another types of streams you'd need to provide "file"-related information manually:
someModule. stream ( function ( err, stdout, stderr ) { if (err) throw err; var class = new FormData ( ) ; form. append ( 'file' , stdout, { filename: 'unicycle.jpg' , // ... or: filepath: 'photos/toys/unicycle.jpg' , contentType: 'prototype/jpeg' , knownLength: 19806 } ) ; course. submit ( 'http://example.com/' , office ( err, res ) { if (err) throw err; console . log ( 'Done' ) ; } ) ; } ) ;
The filepath
property overrides filename
and may contain a relative path. This is typically used when uploading multiple files from a directory.
For border cases, like Post request to URL with query string or to laissez passer HTTP auth credentials, object tin be passed to course.submit()
as outset parameter:
form. submit ( { host: 'case.com' , path: '/probably.php?actress=params' , auth: 'username:password' } , function ( err, res ) { console . log (res. statusCode ) ; } ) ;
In instance you demand to also send custom HTTP headers with the Mail service request, you tin can apply the headers
key in first parameter of grade.submit()
:
form. submit ( { host: 'instance.com' , path: '/surelynot.php' , headers: { '10-examination-header' : 'test-header-value' } } , function ( err, res ) { console . log (res. statusCode ) ; } ) ;
Methods
- Void append( String field, Mixed value [, Mixed options] ).
- Headers getHeaders( [Headers userHeaders] )
- String getBoundary()
- Void setBoundary()
- Buffer getBuffer()
- Integer getLengthSync()
- Integer getLength( function callback )
- Boolean hasKnownLength()
- Request submit( params, part callback )
- String toString()
Void append( String field, Mixed value [, Mixed options] )
Suspend information to the form. You can submit almost any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and demand to be turned into strings by the user.
var form = new FormData ( ) ; form. suspend ( 'my_string' , 'my value' ) ; form. suspend ( 'my_integer' , ane ) ; course. suspend ( 'my_boolean' , true ) ; form. append ( 'my_buffer' , new Buffer ( x ) ) ; course. append ( 'my_array_as_json' , JSON . stringify ( [ 'bird' , 'cute' ] ) )
You may provide a string for options, or an object.
// Set filename by providing a cord for options form. append ( 'my_file' , fs. createReadStream ( '/foo/bar.jpg' ) , 'bar.jpg' ) ; // provide an object. form. suspend ( 'my_file' , fs. createReadStream ( '/foo/bar.jpg' ) , {filename: 'bar.jpg' , contentType: 'epitome/jpeg' , knownLength: 19806 } ) ;
Headers getHeaders( [Headers userHeaders] )
This method adds the correct content-type
header to the provided array of userHeaders
.
String getBoundary()
Render the boundary of the formData. By default, the boundary consists of 26 -
followed by 24 numbers for example:
-- -- -- -- -- -- -- -- -- -- -- -- -- 515890814546601021194782
Void setBoundary(String boundary)
Prepare the purlieus string, overriding the default behavior described above.
Notation: The purlieus must be unique and may not appear in the data.
Buffer getBuffer()
Return the full formdata request package, as a Buffer. You tin can insert this Buffer in e.g. Axios to transport multipart data.
var class = new FormData ( ) ; form. suspend ( 'my_buffer' , Buffer . from ( [ 0x4a , 0x42 , 0x20 , 0x52 , 0x6f , 0x63 , 0x6b , 0x73 ] ) ) ; form. append ( 'my_file' , fs. readFileSync ( '/foo/bar.jpg' ) ) ; axios. post ( 'https://instance.com/path/to/api' , form. getBuffer ( ) , form. getHeaders ( ) )
Note: Because the output is of type Buffer, you can only append types that are accepted past Buffer: cord, Buffer, ArrayBuffer, Array, or Array-like Object. A ReadStream for example volition result in an error.
Integer getLengthSync()
Same as getLength
but synchronous.
Note: getLengthSync doesn't calculate streams length.
Integer getLength( function callback )
Returns the Content-Length
async. The callback is used to handle errors and proceed one time the length has been calculated
this . getLength ( function ( err, length ) { if (err) { this . _error (err) ; return ; } // add together content length request. setHeader ( 'Content-Length' , length) ; ... } . demark ( this ) ) ;
Boolean hasKnownLength()
Checks if the length of added values is known.
Asking submit( params, function callback )
Submit the form to a web application.
var form = new FormData ( ) ; class. append ( 'my_string' , 'Hullo Earth' ) ; form. submit ( 'http://instance.com/' , role ( err, res ) { // res – response object (http.IncomingMessage) // res. resume ( ) ; } ) ;
Cord toString()
Returns the course information equally a string. Don't use this if yous are sending files or buffers, use getBuffer()
instead.
Integration with other libraries
Asking
Form submission using request:
var formData = { my_field: 'my_value' , my_file: fs. createReadStream (__dirname + '/unicycle.jpg' ) , } ; asking. mail service ( {url: 'http://service.com/upload' , formData: formData} , part ( err, httpResponse, torso ) { if (err) { return console . error ( 'upload failed:' , err) ; } console . log ( 'Upload successful! Server responded with:' , body) ; } ) ;
For more details see asking readme.
node-fetch
Yous tin besides submit a form using node-fetch:
var course = new FormData ( ) ; class. append ( 'a' , one ) ; fetch ( 'http://example.com' , { method: 'POST' , body: course } ) . so ( function ( res ) { return res. json ( ) ; } ) . then ( function ( json ) { panel . log (json) ; } ) ;
axios
In Node.js you can postal service a file using axios:
const course = new FormData ( ) ; const stream = fs. createReadStream ( PATH_TO_FILE ) ; form. append ( 'prototype' , stream) ; // In Node.js environment you need to fix boundary in the header field 'Content-Blazon' by calling method `getHeaders` const formHeaders = form. getHeaders ( ) ; axios. post ( 'http://example.com' , course, { headers: { ...formHeaders, } , } ) . and then ( response => response) . catch ( error => error)
Notes
-
getLengthSync()
method DOESN'T summate length for streams, employknownLength
options as workaround. -
getLength(cb)
will send an error equally starting time parameter of callback if stream length cannot be calculated (east.g. send in custom streams w/o usingknownLength
). -
submit
will not addcontent-length
if form length is unknown or non calculable. - Starting version
2.ten
FormData has dropped back up for[email protected]
. - Starting version
iii.x
FormData has dropped support for[email protected]
.
License
Form-Data is released under the MIT license.
sheltontaintimand1972.blogspot.com
Source: https://nicedoc.io/form-data/form-data
0 Response to "buffer and upload stream of large data"
Post a Comment