Gathering detailed insights and metrics for multer-uploader
Gathering detailed insights and metrics for multer-uploader
Gathering detailed insights and metrics for multer-uploader
Gathering detailed insights and metrics for multer-uploader
npm install multer-uploader
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
1 Stars
7 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Aug 05, 2022
Latest Version
1.0.9
Package Id
multer-uploader@1.0.9
Unpacked Size
9.67 kB
Size
3.50 kB
File Count
3
NPM Version
8.19.2
Node Version
19.0.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
Multer-Uploader is a node.js middleware for handling multipart/form-data
, which is primarily used for uploading files. It is written
on top of multer for maximum efficiency.
NOTE: Multer-Uploader will not process any form which is not multipart (multipart/form-data
).
1$ npm install --save multer-uploader
Multer-Uploader adds a body
object and a file
or files
object to the request
object. The body
object contains the values of the text fields of the form, the file
or files
object contains the files uploaded via the form.
Basic usage example:
Don't forget the enctype="multipart/form-data"
in your form.
1<form action="/profile" method="post" enctype="multipart/form-data"> 2 <input type="file" name="avatar" /> 3</form>
1const express = require("express"); 2const upload = require("multer-uploader"); 3 4const app = express(); 5 6// absolute path of your upload directory 7const upload_dir = path.join(__dirname, "/public/uploads"); // absolute path 8// max file size in bytes, such as 1MB equal 1000000 bytes 9const max_file_size = 1000000; // bytes 10// allowed file types in Array 11const allowed_file_mime_type = ["image/png", "image/jpg", "image/jpeg"]; // mime types Array 12 13app.post( 14 "/profile", 15 upload(upload_dir, max_file_size, allowed_file_mime_type).single("avatar"), 16 function (req, res, next) { 17 // req.file is the `avatar` file 18 // req.body will hold the text fields, if there were any 19 } 20); 21 22app.post( 23 "/photos/upload", 24 upload(upload_dir, max_file_size, allowed_file_mime_type).array("photos", 2), 25 function (req, res, next) { 26 // req.files is array of `photos` files 27 // req.body will contain the text fields, if there were any 28 } 29); 30 31const cpUpload = upload( 32 upload_dir, 33 max_file_size, 34 allowed_file_mime_type 35).fields([ 36 { name: "avatar", maxCount: 1 }, 37 { name: "gallery", maxCount: 8 }, 38]); 39app.post("/cool-profile", cpUpload, function (req, res, next) { 40 // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files 41 // 42 // e.g. 43 // req.files['avatar'][0] -> File 44 // req.files['gallery'] -> Array 45 // 46 // req.body will contain the text fields, if there were any 47});
In case you need to handle a text-only multipart form, you should use the .none()
method:
1const express = require("express"); 2const app = express(); 3const upload = require("multer-uploader"); 4 5app.post("/profile", upload().none(), function (req, res, next) { 6 // req.body contains the text fields 7});
Here's an example on how multer-uploader is used an HTML form. Take special note of the enctype="multipart/form-data"
and name="uploaded_file"
fields:
1<form action="/stats" enctype="multipart/form-data" method="post"> 2 <div class="form-group"> 3 <input type="file" class="form-control-file" name="uploaded_file" /> 4 <input 5 type="text" 6 class="form-control" 7 placeholder="Number of speakers" 8 name="nspeakers" 9 /> 10 <input type="submit" value="Get me the stats!" class="btn btn-default" /> 11 </div> 12</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:
1const upload = require("multer-uploader"); 2 3// absolute path of your upload directory 4const upload_dir = path.join(__dirname, "/public/uploads"); // absolute path 5// max file size in bytes, such as 1MB equal 1000000 bytes 6const max_file_size = 1000000; // bytes 7// allowed file types in Array 8const allowed_file_mime_type = ["image/png", "image/jpg", "image/jpeg"]; // mime types Array 9 10app.post( 11 "/stats", 12 upload(upload_dir, max_file_size, allowed_file_mime_type).single( 13 "uploaded_file" 14 ), 15 function (req, res) { 16 // req.file is the name of your file in the form above, here 'uploaded_file' 17 // req.body will hold the text fields, if there were any 18 console.log(req.file, req.body); 19 } 20);
Each file contains the following information:
Key | Description |
---|---|
fieldname | Field name specified in the form |
originalname | Name of the file on the user's computer |
encoding | Encoding type of the file |
mimetype | Mime type of the file |
size | Size of the file in bytes |
destination | The folder to which the file has been saved |
filename | The name of the file within the destination |
path | The full path to the uploaded file |
buffer | A Buffer of the entire file |
.single(fieldname)
Accept a single file with the name fieldname
. The single file will be stored
in req.file
.
.array(fieldname[, maxCount])
Accept an array of files, all with the name fieldname
. Optionally error out if
more than maxCount
files are uploaded. The array of files will be stored in
req.files
.
.fields(fields)
Accept a mix of files, specified by fields
. An object with arrays of files
will be stored in req.files
.
fields
should be an array of objects with name
and optionally a maxCount
.
Example:
1[ 2 { name: "avatar", maxCount: 1 }, 3 { name: "gallery", maxCount: 8 }, 4];
.none()
Accept only text fields. If any file upload is made, error with code "LIMIT_UNEXPECTED_FILE" will be issued.
.any()
Accepts all files that comes over the wire. An array of files will be stored in
req.files
.
WARNING: Make sure that you always handle the files that a user uploads. Never add multer as a global middleware since a malicious user could upload files to a route that you didn't anticipate. Only use this function on routes where you are handling the uploaded files.
When encountering an error, Multer will delegate the error to Express. You can display a nice error page using the standard express way.
If you want to catch errors specifically from Multer, you can call the
middleware function by yourself. Also, if you want to catch only the Multer errors, you can use the MulterError
class that is attached to the multer
object itself (e.g. err instanceof multer.MulterError
).
1const upload = require("multer-uploader"); 2const uploader = upload.single("avatar"); 3 4app.post("/profile", function (req, res) { 5 uploader(req, res, function (err) { 6 if (err instanceof multer.MulterError) { 7 // A Multer error occurred when uploading. 8 } else if (err) { 9 // An unknown error occurred when uploading. 10 } 11 12 // Everything went fine. 13 }); 14});
No vulnerabilities found.
No security vulnerabilities found.