当前位置:网站首页>File expert ---multer

File expert ---multer

2022-06-22 09:20:00 Jdoit CW

Content

master multer Usage of modules

multer

A very easy to use file upload module , Official address :https://www.npmjs.com/package/multer
meanwhile formidable The module is also easy to use , Recommended reading :formidable Use

1. usage

Here is a summary of common usage in the project :

  1. First installation multer modular
npm install --save multer
  1. Import this module and perform basic configuration
const multer = require('multer');
const upload = multer({
     dest: './public/uploads' }).single('file'); // Upload a single file 

app.post('/multertest', upload, (req, res) => {
    
    res.send(req.file)
})

Be careful : Here uploads The path is relative to the server

  1. Test interface ( Take the picture )

①: use Postman To test

 Insert picture description here

②: Simulate submission with a real form

Form code :

Be sure to write... Here :enctype="multipart/form-data"

 <form action="/multertest" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value=" Submit ">
    </form>

Submit results :
 Insert picture description here

  1. The server uploads Folder

 Insert picture description here

summary : You can see uploads The pictures under the folder have no suffix , If stored in the database , Low controllability , So we need to be right file Uploaded files To deal with

2. Information processing

In the actual project , We often want to upload pictures to Path to the server , And then put The path is stored in the database , When used , Request picture path , write in src attribute .

①:node Example :

const multer = require('multer');
const fs = require('fs');
const upload = multer({
     dest: './public/uploads' }).single('file');

app.post('/multertest', upload, (req, res) => {
    
    let file = req.file;
    // fs.renameSync(oldname,newname); This method is used to change the file name ( Of documents oldname And newname Both in file In the object )
    fs.renameSync('./public/uploads/' + file.filename, './public/uploads/' + file.originalname);
    let imgurl = 'http://localhost:3000/uploads/' + file.originalname;
    res.send(imgurl)
})

②:Postman test

 Insert picture description here

③: The server uploads Folder

 Insert picture description here

At this point, whether it is stored in the database , Or direct access , All become simple and direct .

3. Multiple file upload

Multi file uploads are similar to single file uploads , It's just When creating path parameters , Limit it , The demonstration is brief , Go straight to the example

Example :

const multer = require('multer');
const fs = require('fs');
let uploadMore = multer({
     dest: './public/uploads' }).array('file', 5);//  Here to 5 For example ( Number of uploads <= 5)

app.post('/multertest', uploadMore , (req, res) => {
    
  let files = req.files;
  for (var k in files) {
    
            let file = files[k];
            fs.renameSync('./public/uploads/' + file.filename, './public/uploads/' + file.originalname);
            let url = 'http:localhost:3000/uploads' + file.originalname;
            }
})

over

原网站

版权声明
本文为[Jdoit CW]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220844301588.html