Multer - Build RestAPI to upload a MultipartFile to NodeJS/Express
multer
3
nodejs
106
express
15
restapi
16
Male avatar

loveprogramming viết ngày 03/04/2021

https://grokonez.com/node-js/multer-build-restapi-to-upload-a-multipartfile-to-nodejs-express

Multer - Build RestAPI to upload a MultipartFile to NodeJS/Express

table, th, td { border: 1px solid black; border-collapse: collapse; } tr:nth-child(even) { background-color: #e6eeff; } th, td { padding: 5px; } th { text-align: center; }

In the tutorial, we show how to use Multer middleware for building a RestAPI to upload MultipartFile to NodeJS/Express web-application.

Related posts:

Multer - Upload MultipartFile

Multer is a NodeJS middleware for handling multipart/form-data. It will not process any form which is not multipart (multipart/form-data). To install Multer module, use below command:

npm install multer --save

Follow below segment code to build RestAPIs for handling a uploaded file:


const multer = require('multer');

var storage = multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, __basedir + '/uploads/')
    },
    filename: (req, file, cb) => {
      cb(null, file.fieldname + "-" + Date.now() + "-" + file.originalname)
    }
});

var upload = multer({storage: storage});

app.post('/api/uploadfile', upload.single("uploadfile"), (req, res) => {
  console.log(req.file);
  res.json({'msg': 'File uploaded successfully!', 'file': req.file});
});

Multer provides a diskStorage engine that we have a full control on storing files to disk.

var storage = multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, __basedir + '/uploads/')
    },
    filename: (req, file, cb) => {
      cb(null, file.fieldname + "-" + Date.now() + "-" + file.originalname)
    }
});

var upload = multer({storage: storage});
  • destination is used to determine within which folder the uploaded files should be stored.
  • filename is used to determine what the file should be named inside the folder.

How the below segment code work?

app.post('/api/uploadfile', upload.single("uploadfile"), (req, res) => {
    ...
});

-> Multer adds a file object to the request object. req.file is the uploadfile file.

https://grokonez.com/node-js/multer-build-restapi-to-upload-a-multipartfile-to-nodejs-express

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

127 bài viết.
60 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 4 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 4 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/nodejs/multer/nodejsexpressuploadtextfieldsmultipartfilewithmulterjqueryajaxbootstrap4 NodeJS/Express – Upload TextFields + M...
loveprogramming viết 20 giờ trước
0 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 4 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
127 bài viết.
60 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!