UNPKG – express-fileupload
1
# express-fileupload
2
Simple express middleware for uploading files.
3
4
[
![npm
](
https://img.shields.io/npm/v/express-fileupload.svg
)](
https://www.npmjs.org/package/express-fileupload
)
5
[
![Build Status
](
https://travis-ci.org/richardgirges/express-fileupload.svg?branch=master
)](
https://travis-ci.org/richardgirges/express-fileupload
)
6
[
![downloads per month
](
http://img.shields.io/npm/dm/express-fileupload.svg
)](
https://www.npmjs.org/package/express-fileupload
)
7
[
![Coverage Status
](
https://img.shields.io/coveralls/richardgirges/express-fileupload.svg
)](
https://coveralls.io/r/richardgirges/express-fileupload
)
8
9
# Version 0.1.0 Breaking Changes!
10
11
#### » No more urlencoded support
12
As of
`v0.1.0`
, there is NO MORE
`application/x-www-form-urlencoded`
SUPPORT! Moving forward, express-fileupload is considered a "multipart" solution only.
13
14
If you want to parse
`urlencoded`
requests, [
use body-parser
](
https://github.com/expressjs/body-parser#bodyparserurlencodedoptions
).
15
16
#### » No more support for < Node.js v4
17
No more support for versions of Node older than v4. Use with lower versions of Node at your own risk!
18
19
# Install
20
```bash
21
# With NPM
22
npm install --save express-fileupload
23
24
# With Yarn
25
yarn add express-fileupload
26
```
27
28
# Usage
29
When you upload a file, the file will be accessible from
`req.files`
.
30
31
### Example Scenario
32
*
You're uploading a file called
**car.jpg**
33
*
Your input's name field is
**foo**
:
`<input name="foo" type="file" />`
34
*
In your express server request, you can access your uploaded file from
`req.files.foo`
:
35
```javascript
36
app.post('/upload', function(req, res) {
37
console.log(req.files.foo); // the uploaded file object
38
});
39
```
40
The
**req.files.foo**
object will contain the following:
41
*
`req.files.foo.name`
: "car.jpg"
42
*
`req.files.foo.mv`
: A function to move the file elsewhere on your server
43
*
`req.files.mimetype`
: The mimetype of your file
44
*
`req.files.data`
: A buffer representation of your file
45
46
### Full Example
47
**Your node.js code:**
48
```javascript
49
const express = require('express');
50
const fileUpload = require('express-fileupload');
51
const app = express();
52
53
// default options
54
app.use(fileUpload());
55
56
app.post('/upload', function(req, res) {
57
if (!req.files)
58
return res.status(400).send('No files were uploaded.');
59
60
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
61
let sampleFile = req.files.sampleFile;
62
63
// Use the mv() method to place the file somewhere on your server
64
sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
65
if (err)
66
return res.status(500).send(err);
67
68
res.send('File uploaded!');
69
});
70
});
71
```
72
73
**Your HTML file upload form:**
74
```html
75
<html>
76
<body>
77
<form ref='uploadForm'
78
id='uploadForm'
79
action='http://localhost:8000/upload'
80
method='post'
81
encType="multipart/form-data">
82
<input type="file" name="sampleFile" />
83
<input type='submit' value='Upload!' />
84
</form>
85
</body>
86
</html>
87
```
88
89
### Uploading Multiple Files
90
express-fileupload supports multiple file uploads at the same time.
91
92
Let's say you have three files in your form, each of the inputs with the name
`my_profile_pic`
,
`my_pet`
, and
`my_cover_photo`
:
93
```html
94
<input type="file" name="my_profile_pic" />
95
<input type="file" name="my_pet" />
96
<input type="file" name="my_cover_photo" />
97
```
98
99
These uploaded files would be accessible like so:
100
```javascript
101
app.post('/upload', function(req, res) {
102
// Uploaded files:
103
console.log(req.files.my_profile_pic.name);
104
console.log(req.files.my_pet.name);
105
console.log(req.files.my_cover_photo.name);
106
});
107
```
108
109
### Using Busboy Options
110
Pass in Busboy options directly to the express-fileupload middleware. [
Check out the Busboy documentation here.
](
https://github.com/mscdex/busboy#api
)
111
112
```javascript
113
app.use(fileUpload({
114
limits: { fileSize: 50 * 1024 * 1024 },
115
}));
116
```
117
118
### Available Options
119
Pass in non-Busboy options directly to the middleware. These are express-fileupload specific options.
120
121
Option | Acceptable Values | Details
122
--- | --- | ---
123
safeFileNames |
<
ul
>
<
li
>
<
code
>false
</
code
>
**(default)**
</
li
>
<
li
>
<
code
>true
</
code
>
</
li
>
<
li
>regex
</
li
>
</
ul
> | Strips characters from the upload's filename. You can use custom regex to determine what to strip. If set to
`true`
, non-alphanumeric characters
_except_
dashes and underscores will be stripped. This option is off by default.
<
br
/>
<
br
/>
**Example #1 (strip slashes from file names):**
`app.use(fileUpload({ safeFileNames: /\\/g }))`
<
br
/>
**Example #2:**
`app.use(fileUpload({ safeFileNames: true }))`
124
125
# Help Wanted
126
Pull Requests are welcomed!
127
128
# Thanks & Credit
129
[
Brian White
](
https://github.com/mscdex
) for his stellar work on the [
Busboy Package
](
https://github.com/mscdex/busboy
) and the [
connect-busboy Package
](
https://github.com/mscdex/connect-busboy
)