Comparing version 0.0.6 to 0.0.7
18
index.js
@@ -23,3 +23,3 @@ var os = require('os'); | ||
mkdirp(dest, function(err) { if (err) throw err; }); | ||
// renaming function for the uploaded file - need not worry about the extension | ||
@@ -35,6 +35,6 @@ // ! if you want to keep the original filename, write a renamer function which does that | ||
req.body = req.body || {}; | ||
req.files = req.files || {}; | ||
req.files = req.files || {}; | ||
if (req.headers['content-type'] && | ||
req.headers['content-type'].indexOf('multipart/form-data') === 0 && | ||
if (req.headers['content-type'] && | ||
req.headers['content-type'].indexOf('multipart/form-data') === 0 && | ||
(req.method === 'POST' || req.method === 'PUT') | ||
@@ -47,3 +47,3 @@ ) { | ||
options.headers = req.headers; | ||
var busboy = new Busboy(options); | ||
@@ -107,3 +107,4 @@ | ||
file.truncated = fileStream.truncated; | ||
req.files[fieldname] = file; | ||
if (!req.files[fieldname]) { req.files[fieldname] = []; } | ||
req.files[fieldname].push(file); | ||
// trigger "file end" event | ||
@@ -140,2 +141,7 @@ if (options.onFileUploadComplete) { options.onFileUploadComplete(file); } | ||
busboy.on('end', function() { | ||
for (var field in req.files){ | ||
if (req.files[field].length===1){ | ||
req.files[field] = req.files[field][0]; | ||
} | ||
} | ||
// when done parsing the form, pass the control to the next middleware in stack | ||
@@ -142,0 +148,0 @@ if (options.onParseEnd) { options.onParseEnd(); } |
{ | ||
"name": "multer", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Connect middleware for handling multipart/form-data", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
192
README.md
@@ -1,25 +0,31 @@ | ||
Multer | ||
====== | ||
# Multer [![Build Status](https://travis-ci.org/expressjs/multer.svg?branch=master)](https://travis-ci.org/expressjs/multer) [![NPM version](https://badge.fury.io/js/multer.svg)](https://badge.fury.io/js/multer) | ||
Multer is a Connect / Express middleware for handling **multipart/form-data**. It is written on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency. | ||
Multer is a node.js middleware for handling `multipart/form-data`. | ||
## Usage | ||
It is written on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency. | ||
Install the Multer package from npm: | ||
## API | ||
$ npm install multer | ||
#### Installation | ||
Include the Multer middleware in your app: | ||
`$ npm install multer` | ||
... | ||
var multer = require('multer'); | ||
app.use(multer({ dest: './uploads/'})); | ||
... | ||
#### Usage | ||
```js | ||
var express = require('express') | ||
var multer = require('multer') | ||
var app = express() | ||
app.use(multer({ dest: './uploads/'})) | ||
``` | ||
You can access the fields and files in the `request` object: | ||
console.log(req.body); | ||
console.log(req.files); | ||
```js | ||
console.log(req.body) | ||
console.log(req.files) | ||
``` | ||
**IMPORTANT**: Multer will not process any form which is not **multipart/form-data** submitted via the **POST** or **PUT** methods. | ||
**IMPORTANT**: Multer will not process any form which is not `multipart/form-data` submitted via the `POST` or `PUT` methods. | ||
@@ -65,21 +71,21 @@ ## Multer file object | ||
app.use(multer({ | ||
dest: './uploads/', | ||
rename: function(fieldname, filename) { | ||
return filename.replace(/\W+/g, '-').toLowerCase() + Date.now(); | ||
} | ||
})); | ||
```js | ||
app.use(multer({ | ||
dest: './uploads/', | ||
rename: function (fieldname, filename) { | ||
return filename.replace(/\W+/g, '-').toLowerCase() + Date.now() | ||
} | ||
})) | ||
``` | ||
The details of the properties of the options object is explained in the following sections. | ||
###dest | ||
### dest | ||
The destination directory for the uploaded files. | ||
Example: | ||
`dest: './uploads/'` | ||
dest: './uploads/' | ||
### limits | ||
###limits | ||
An object specifying the size limits of the following optional properties. | ||
@@ -95,113 +101,113 @@ | ||
Example: | ||
```js | ||
limits: { | ||
fieldNameSize: 100, | ||
files: 2, | ||
fields: 5 | ||
} | ||
``` | ||
limits: { | ||
fieldNameSize: 100, | ||
files: 2, | ||
fields: 5 | ||
} | ||
Specifying the limits can help protect your site against denial of service (DoS) attacks. | ||
###rename(fieldname, filename) | ||
### rename(fieldname, filename) | ||
Function to rename the uploaded files. Whatever the function returns will become the new name of the uploaded file (extension is not included). The `fieldname` and `filename` of the file will be available in this function, use them if you need to. | ||
Example: | ||
```js | ||
rename: function (fieldname, filename) { | ||
return fieldname + filename + Date.now() | ||
} | ||
``` | ||
rename: function(fieldname, filename) { | ||
return fieldname + filename + Date.now(); | ||
} | ||
### onFileUploadStart(file) | ||
###onFileUploadStart(file) | ||
Event handler triggered when a file starts to be uploaded. A file object with the following properties are available to this function: `fieldname`, `originalname`, `name`, `encoding`, `mimetype`, `path`, `extension`. | ||
Example: | ||
```js | ||
onFileUploadStart: function (file) { | ||
console.log(file.fieldname + ' is starting ...') | ||
} | ||
``` | ||
onFileUploadStart: function(file) { | ||
console.log(file.fieldname + ' is starting ...'); | ||
} | ||
### onFileUploadData(file, data) | ||
###onFileUploadData(file, data) | ||
Event handler triggered when a chunk of buffer is received. A buffer object along with a file object is available to the function. | ||
Example: | ||
```js | ||
onFileUploadData: function (file, data) { | ||
console.log(data.length + ' of ' + file.fieldname + ' arrived') | ||
} | ||
``` | ||
onFileUploadData: function(file, data) { | ||
console.log(data.length + ' of ' + file.fieldname + ' arrived'); | ||
} | ||
### onFileUploadComplete(file) | ||
###onFileUploadComplete(file) | ||
Event handler trigger when a file is completely uploaded. A file object is available to the function. | ||
Example: | ||
```js | ||
onFileUploadComplete: function (file) { | ||
console.log(file.fieldname + ' uploaded to ' + file.path) | ||
} | ||
``` | ||
onFileUploadComplete: function(file) { | ||
console.log(file.fieldname + ' uploaded to ' + file.path); | ||
} | ||
### onParseStart() | ||
###onParseStart() | ||
Event handler triggered when the form parsing starts. | ||
Example: | ||
```js | ||
onParseStart: function () { | ||
console.log('Form parsing started at: ', new Date()) | ||
} | ||
``` | ||
onParseStart: function() { | ||
console.log('Form parsing started at: ', new Date()); | ||
} | ||
### onParseEnd() | ||
###onParseEnd() | ||
Event handler triggered when the form parsing completes. | ||
Example: | ||
```js | ||
onParseStart: function () { | ||
console.log('Form parsing completed at: ', new Date()) | ||
} | ||
``` | ||
onParseStart: function() { | ||
console.log('Form parsing completed at: ', new Date()); | ||
} | ||
### onError() | ||
###onError() | ||
Event handler for any errors encountering while processing the form. The `error` object and the `next` object is available to the function. If you are handling errors yourself, make sure to terminate the request or call the `next()` function, else the request will be left hanging. | ||
Example: | ||
```js | ||
onError: function (error, next) { | ||
console.log(error) | ||
next(error) | ||
} | ||
``` | ||
onError: function(error, next) { | ||
console.log(error); | ||
next(error); | ||
} | ||
### onFilesLimit() | ||
###onFilesLimit() | ||
Event handler triggered when the number of files exceed the specification in the `limit` object. No more files will be parsed after the limit is reached. | ||
Example: | ||
```js | ||
onFilesLimit: function () { | ||
console.log('Crossed file limit!') | ||
} | ||
``` | ||
onFilesLimit = function() { | ||
console.log('Crossed file limit!'); | ||
}; | ||
### onFieldsLimit() | ||
###onFieldsLimit() | ||
Event handler triggered when the number of fields exceed the specification in the `limit` object. No more fields will be parsed after the limit is reached. | ||
Example: | ||
```js | ||
onFilesLimit: function () { | ||
console.log('Crossed fields limit!') | ||
} | ||
``` | ||
onFilesLimit = function() { | ||
console.log('Crossed fields limit!'); | ||
}; | ||
### onPartsLimit() | ||
###onPartsLimit() | ||
Event handler triggered when the number of parts exceed the specification in the `limit` object. No more files or fields will be parsed after the limit is reached. | ||
Example: | ||
```js | ||
onFilesLimit: function () { | ||
console.log('Crossed parts limit!') | ||
} | ||
``` | ||
onFilesLimit = function() { | ||
console.log('Crossed parts limit!'); | ||
}; | ||
## License (MIT) | ||
@@ -208,0 +214,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
534052
327
230