Welcome Folks today in this tutorial we will be uploading files in Node.js Express Application using fileupload library. The step by step video is shown below to follow the instructions to make this application
express-fileupload
Simple express middleware for uploading files.
Install
1 2 3 4 5 6 |
Install # With NPM npm i express-fileupload # With Yarn yarn add express-fileupload |
Usage
When you upload a file, the file will be accessible from req.files
.
- You’re uploading a file called car.jpg
- Your input’s name field is foo:
<input name="foo" type="file" />
- In your express server request, you can access your uploaded file from
req.files.foo
:
1 2 3 |
app.post('/upload', function(req, res) { console.log(req.files.foo); // the uploaded file object }); |
The req.files.foo object will contain the following:
req.files.foo.name
: “car.jpg”req.files.foo.mv
: A function to move the file elsewhere on your server. Can take a callback or return a promise.req.files.foo.mimetype
: The mimetype of your filereq.files.foo.data
: A buffer representation of your file, returns empty buffer in case useTempFiles option was set to true.req.files.foo.tempFilePath
: A path to the temporary file in case useTempFiles option was set to true.req.files.foo.truncated
: A boolean that represents if the file is over the size limitreq.files.foo.size
: Uploaded size in bytesreq.files.foo.md5
: MD5 checksum of the uploaded file
1 2 3 |
app.use(fileUpload({ limits: { fileSize: 50 * 1024 * 1024 }, })); |
1 2 3 4 |
app.use(fileUpload({ useTempFiles : true, tempFileDir : '/tmp/' })); |