You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple express file upload middleware that wraps around [connect-busboy](https://github.com/mscdex/connect-busboy).
1
+
# Description
2
+
Simple express middleware for uploading files.
3
3
4
-
##Install
4
+
# Install
5
5
```bash
6
-
npm install express-fileupload
7
-
```
6
+
# With NPM
7
+
npm install --save express-fileupload
8
8
9
-
## Important Note
10
-
Add `app.use(fileUpload())`*AFTER*`app.use(bodyParser.json)` and any other bodyParser middlewares! This limitation will be explored and resolved in an upcoming release.
9
+
# With Yarn
10
+
yarn add express-fileupload
11
+
```
11
12
12
-
=======
13
-
Pass in Busboy options directly to express-fileupload (using Busboy `v0.2.13`). Check out the Busboy documentation here: https://github.com/mscdex/busboy#api
13
+
# Usage
14
+
When you upload a file, the file will be accessible from `req.files`.
14
15
16
+
### Example Scenario
17
+
* You're uploading a file called **car.jpg**
18
+
* Your input's name field is **foo**: `<input name="foo" type="file" />`
19
+
* In your express server request, you can access your uploaded file from `req.files.foo`:
15
20
```javascript
16
-
app.use(fileUpload({
17
-
limits: { fileSize:50*1024*1024 },
18
-
}));
21
+
app.post('/upload', function(req, res) {
22
+
console.log(req.files.foo); // the uploaded file object
23
+
});
19
24
```
25
+
The **req.files.foo** object will contain the following:
26
+
*`req.files.foo.name`: "car.jpg"
27
+
*`req.files.foo.mv`: A function to move the file elsewhere on your server
28
+
*`req.files.mimetype`: The mimetype of your file
29
+
*`req.files.data`: A buffer representation of your file
Pass in Busboy options directly to the express-fileupload middleware. [Check out the Busboy documentation here.](https://github.com/mscdex/busboy#api)
82
+
83
+
```javascript
84
+
app.use(fileUpload({
85
+
limits: { fileSize:50*1024*1024 },
86
+
}));
87
+
```
88
+
89
+
### Available Options
90
+
Pass in non-Busboy options directly to the middleware. These are express-fileupload specific options.
91
+
92
+
Option | Acceptable Values | Details
93
+
--- | --- | ---
94
+
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 out. 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 out slashes from file names):**`app.use(fileUpload({ safeFileNames: /\\/g }))`<br />**Example #2:**`app.use(fileUpload({ safeFileNames: true }))`
95
+
96
+
# Known Bugs
97
+
##### If you're using bodyParser middleware
98
+
Add `app.use(fileUpload())`*AFTER*`app.use(bodyParser.json)` and any other `bodyParser` middlewares! This limitation will be investigated in an upcoming release.
99
+
100
+
# Help Wanted!
101
+
Pull Requests are welcomed!
70
102
71
-
*[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)
103
+
# Thanks & Credit
104
+
[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)
0 commit comments