Comparing version 1.4.2 to 1.4.3
@@ -88,3 +88,3 @@ var is = require('type-is') | ||
// Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6) | ||
if (limits && limits.hasOwnProperty('fieldNameSize')) { | ||
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) { | ||
if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY') | ||
@@ -102,3 +102,3 @@ } | ||
// Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6) | ||
if (limits && limits.hasOwnProperty('fieldNameSize')) { | ||
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) { | ||
if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY') | ||
@@ -105,0 +105,0 @@ } |
var util = require('util') | ||
var errorMessages = { | ||
'LIMIT_PART_COUNT': 'Too many parts', | ||
'LIMIT_FILE_SIZE': 'File too large', | ||
'LIMIT_FILE_COUNT': 'Too many files', | ||
'LIMIT_FIELD_KEY': 'Field name too long', | ||
'LIMIT_FIELD_VALUE': 'Field value too long', | ||
'LIMIT_FIELD_COUNT': 'Too many fields', | ||
'LIMIT_UNEXPECTED_FILE': 'Unexpected field' | ||
LIMIT_PART_COUNT: 'Too many parts', | ||
LIMIT_FILE_SIZE: 'File too large', | ||
LIMIT_FILE_COUNT: 'Too many files', | ||
LIMIT_FIELD_KEY: 'Field name too long', | ||
LIMIT_FIELD_VALUE: 'Field value too long', | ||
LIMIT_FIELD_COUNT: 'Too many fields', | ||
LIMIT_UNEXPECTED_FILE: 'Unexpected field' | ||
} | ||
@@ -12,0 +12,0 @@ |
{ | ||
"name": "multer", | ||
"description": "Middleware for handling `multipart/form-data`.", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"contributors": [ | ||
@@ -25,3 +25,3 @@ "Hage Yaapa <captain@hacksparrow.com> (http://www.hacksparrow.com)", | ||
"concat-stream": "^1.5.2", | ||
"mkdirp": "^0.5.1", | ||
"mkdirp": "^0.5.4", | ||
"object-assign": "^4.1.1", | ||
@@ -33,2 +33,3 @@ "on-finished": "^2.3.0", | ||
"devDependencies": { | ||
"deep-equal": "^2.0.3", | ||
"express": "^4.13.1", | ||
@@ -39,3 +40,3 @@ "form-data": "^1.0.0-rc1", | ||
"rimraf": "^2.4.1", | ||
"standard": "^11.0.1", | ||
"standard": "^14.3.3", | ||
"testdata-w3c-json-form": "^1.0.0" | ||
@@ -42,0 +43,0 @@ }, |
@@ -15,2 +15,3 @@ # 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) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
- [Русский язык](https://github.com/expressjs/multer/blob/master/doc/README-ru.md) (Russian) | ||
- [Português](https://github.com/expressjs/multer/blob/master/doc/README-pt-br.md) (Português Brazil) | ||
@@ -38,7 +39,7 @@ ## Installation | ||
```javascript | ||
var express = require('express') | ||
var multer = require('multer') | ||
var upload = multer({ dest: 'uploads/' }) | ||
const express = require('express') | ||
const multer = require('multer') | ||
const upload = multer({ dest: 'uploads/' }) | ||
var app = express() | ||
const app = express() | ||
@@ -55,3 +56,3 @@ app.post('/profile', upload.single('avatar'), function (req, res, next) { | ||
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]) | ||
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]) | ||
app.post('/cool-profile', cpUpload, function (req, res, next) { | ||
@@ -71,6 +72,6 @@ // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files | ||
```javascript | ||
var express = require('express') | ||
var app = express() | ||
var multer = require('multer') | ||
var upload = multer() | ||
const express = require('express') | ||
const app = express() | ||
const multer = require('multer') | ||
const upload = multer() | ||
@@ -82,2 +83,28 @@ app.post('/profile', upload.none(), function (req, res, next) { | ||
Here's an example on how multer is used an HTML form. Take special note of the `enctype="multipart/form-data"` and `name="uploaded_file"` fields: | ||
```html | ||
<form action="/stats" enctype="multipart/form-data" method="post"> | ||
<div class="form-group"> | ||
<input type="file" class="form-control-file" name="uploaded_file"> | ||
<input type="text" class="form-control" placeholder="Number of speakers" name="nspeakers"> | ||
<input type="submit" value="Get me the stats!" class="btn btn-default"> | ||
</div> | ||
</form> | ||
``` | ||
Then in your javascript file you would add these lines to access both the file and the body. It is important that you use the `name` field value from the form in your upload function. This tells multer which field on the request it should look for the files in. If these fields aren't the same in the HTML form and on your server, your upload will fail: | ||
```javascript | ||
const multer = require('multer') | ||
const upload = multer({ dest: './public/data/uploads/' }) | ||
app.post('/stats', upload.single('uploaded_file'), function (req, res) { | ||
// req.file is the name of your file in the form above, here 'uploaded_file' | ||
// req.body will hold the text fields, if there were any | ||
console.log(req.file, req.body) | ||
}); | ||
``` | ||
## API | ||
@@ -123,3 +150,3 @@ | ||
```javascript | ||
var upload = multer({ dest: 'uploads/' }) | ||
const upload = multer({ dest: 'uploads/' }) | ||
``` | ||
@@ -179,3 +206,3 @@ | ||
```javascript | ||
var storage = multer.diskStorage({ | ||
const storage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
@@ -185,7 +212,8 @@ cb(null, '/tmp/my-uploads') | ||
filename: function (req, file, cb) { | ||
cb(null, file.fieldname + '-' + Date.now()) | ||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9) | ||
cb(null, file.fieldname + '-' + uniqueSuffix) | ||
} | ||
}) | ||
var upload = multer({ storage: storage }) | ||
const upload = multer({ storage: storage }) | ||
``` | ||
@@ -218,2 +246,6 @@ | ||
For understanding the calling convention used in the callback (needing to pass | ||
null as the first param), refer to | ||
[Node.js error handling](https://www.joyent.com/node-js/production/design/errors) | ||
#### `MemoryStorage` | ||
@@ -225,4 +257,4 @@ | ||
```javascript | ||
var storage = multer.memoryStorage() | ||
var upload = multer({ storage: storage }) | ||
const storage = multer.memoryStorage() | ||
const upload = multer({ storage: storage }) | ||
``` | ||
@@ -287,4 +319,4 @@ | ||
```javascript | ||
var multer = require('multer') | ||
var upload = multer().single('avatar') | ||
const multer = require('multer') | ||
const upload = multer().single('avatar') | ||
@@ -291,0 +323,0 @@ app.post('/profile', function (req, res) { |
@@ -8,3 +8,3 @@ var fs = require('fs') | ||
function getFilename (req, file, cb) { | ||
crypto.pseudoRandomBytes(16, function (err, raw) { | ||
crypto.randomBytes(16, function (err, raw) { | ||
cb(err, err ? undefined : raw.toString('hex')) | ||
@@ -11,0 +11,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
27557
332
8
11
Updatedmkdirp@^0.5.4