GithubHelp home page GithubHelp logo

nvdnkpr / node-multiparty Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pillarjs/multiparty

0.0 1.0 0.0 3.23 MB

A node.js module for parsing multipart-form data requests which supports streams2

License: MIT License

node-multiparty's Introduction

multiparty Build Status NPM version

Parse http requests with content-type multipart/form-data, also known as file uploads.

See also busboy - a faster alternative which may be worth looking into.

Why the fork?

  • This module uses the Node.js v0.10 streams properly
  • It will not create a temp file for you unless you want it to.
  • Counts bytes and does math to help you figure out the Content-Length of the final part.
  • You can stream uploads to s3 with aws-sdk, for example.
  • Less bugs. This code is simpler, has all deprecated functionality removed, has cleaner tests, and does not try to do anything beyond multipart stream parsing.

Installation

npm install multiparty

Usage

Parse an incoming multipart/form-data request.

var multiparty = require('multiparty')
  , http = require('http')
  , util = require('util')

http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields, files: files}));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

API

multiparty.Form

var form = new multiparty.Form(options)

Creates a new form. Options:

  • encoding - sets encoding for the incoming form fields. Defaults to utf8.
  • maxFieldsSize - Limits the amount of memory all fields (not files) can allocate in bytes. If this value is exceeded, an error event is emitted. The default size is 2MB.
  • maxFields - Limits the number of fields that will be parsed before emitting an error event. A file counts as a field in this case. Defaults to 1000.
  • maxFilesSize - Only relevant when autoFiles is true. Limits the total bytes accepted for all files combined. If this value is exceeded, an error event is emitted. The default is Infinity.
  • autoFields - Enables field events and disables part events for fields. This is automatically set to true if you add a field listener.
  • autoFiles - Enables file events and disables part events for files. This is automatically set to true if you add a file listener.
  • uploadDir - Only relevant when autoFiles is true. The directory for placing file uploads in. You can move them later using fs.rename(). Defaults to os.tmpDir().

form.parse(request, [cb])

Parses an incoming node.js request containing form data.This will cause form to emit events based off the incoming request.

var count = 0;
var form = new multiparty.Form();

// Errors may be emitted
// Note that if you are listening to 'part' events, the same error may be
// emitted from the `form` and the `part`.
form.on('error', function(err) {
  console.log('Error parsing form: ' + err.stack);
});

// Parts are emitted when parsing the form
form.on('part', function(part) {
  // You *must* act on the part by reading it
  // NOTE: if you want to ignore it, just call "part.resume()"

  if (part.filename === null) {
    // filename is "null" when this is a field and not a file
    console.log('got field named ' + part.name);
    // ignore field's content
    part.resume();
  }

  if (part.filename !== null) {
    // filename is not "null" when this is a file
    count++;
    console.log('got file named ' + part.name);
    // ignore file's content here
    part.resume();
  }

  part.on('error', function(err) {
    // decide what to do
  });
});

// Close emitted after form parsed
form.on('close', function() {
  console.log('Upload completed!');
  res.setHeader('text/plain');
  res.end('Received ' + count + ' files');
});

// Parse req
form.parse(req);

If cb is provided, autoFields and autoFiles are set to true and all fields and files are collected and passed to the callback, removing the need to listen to any events on form. This is for convenience when you want to read everything, but be sure to write cleanup code, as this will write all uploaded files to the disk, even ones you may not be interested in.

form.parse(req, function(err, fields, files) {
  Object.keys(fields).forEach(function(name) {
    console.log('got field named ' + name);
  });

  Object.keys(files).forEach(function(name) {
    console.log('got file named ' + name);
  });

  console.log('Upload completed!');
  res.setHeader('text/plain');
  res.end('Received ' + files.length + ' files');
});

fields is an object where the property names are field names and the values are arrays of field values.

files is an object where the property names are field names and the values are arrays of file objects.

form.bytesReceived

The amount of bytes received for this form so far.

form.bytesExpected

The expected number of bytes in this form.

Events

'error' (err)

Unless you supply a callback to form.parse, you definitely want to handle this event. Otherwise your server will crash when users submit bogus multipart requests!

Only one 'error' event can ever be emitted, and if an 'error' event is emitted, then 'close' will not be emitted.

Note that an 'error' event will be emitted both from the form and from the current part.

'part' (part)

Emitted when a part is encountered in the request. part is a ReadableStream. It also has the following properties:

  • headers - the headers for this part. For example, you may be interested in content-type.
  • name - the field name for this part
  • filename - only if the part is an incoming file
  • byteOffset - the byte offset of this part in the request body
  • byteCount - assuming that this is the last part in the request, this is the size of this part in bytes. You could use this, for example, to set the Content-Length header if uploading to S3. If the part had a Content-Length header then that value is used here instead.

Parts for fields are not emitted when autoFields is on, and likewise parts for files are not emitted when autoFiles is on.

part emits 'error' events! Make sure you handle them.

'aborted'

Emitted when the request is aborted. This event will be followed shortly by an error event. In practice you do not need to handle this event.

'progress' (bytesReceived, bytesExpected)

'close'

Emitted after all parts have been parsed and emitted. Not emitted if an error event is emitted.

If you have autoFiles on, this is not fired until all the data has been flushed to disk and the file handles have been closed.

This is typically when you would send your response.

'file' (name, file)

By default multiparty will not touch your hard drive. But if you add this listener, multiparty automatically sets form.autoFiles to true and will stream uploads to disk for you.

The max bytes accepted per request can be specified with maxFilesSize.

  • name - the field name for this file
  • file - an object with these properties:
    • fieldName - same as name - the field name for this file
    • originalFilename - the filename that the user reports for the file
    • path - the absolute path of the uploaded file on disk
    • headers - the HTTP headers that were sent along with this file
    • size - size of the file in bytes

'field' (name, value)

  • name - field name
  • value - string field value

node-multiparty's People

Contributors

aheckmann avatar amitaibu avatar andreazevedo avatar andrewrk avatar baoshan avatar bcherry avatar bengourley avatar btrask avatar carbonrobot avatar deanlandolt avatar dougwilson avatar egirshov avatar elmerbulthuis avatar felixge avatar foxxtrot avatar igornovozhilov avatar jasondavies avatar jimbly avatar kapouer avatar mikefrey avatar monsendag avatar ncr avatar orangedog avatar scriby avatar sreuter avatar svnlto avatar thanpolas avatar tim-kos avatar tj avatar tojocky avatar

Watchers

 avatar

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.