The front and rear end can upload multiple pictures.

I want to put the pictures in a folder, and then store the addresses of these pictures in the database, and when the page wants to load these pictures, I will take them out according to the address in the database. I would like to ask how to put the pictures in the folder. My backend uses node.js

.

express.js

var express = require('express');
var fs=require('fs'); //
var app=express(); //web
var multer=require('multer'); //Node.jsmultipart/form-data
var upload=multer({dest:'./tmp'});


app.get('/index.html',function (req,res) {
    res.sendfile(__dirname+'/index.html');
});


app.post('/addUserInfo', upload.array("file"), function(req, res, next){
    if(req.files==undefined){
        res.send("...");
    }else{
        var str="...";
        for(var i=0;i<req.files.length;iPP){
            var filepath = __dirname + "/tmp/" + req.files[i].originalname;
            fs.renameSync(req.files[i].path,filepath);
            // 
        }
        res.send("...");
    }
});
app.listen(8080,"127.0.0.1");


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="addUserInfo">
:<input type="file" id="file" name="file" multiple/><br/>
<input type="submit" value=""/>
</form>
</body>
</html>

multer middleware is mainly used to deal with multipart/form-data types
if you want forms and pictures to be submitted together, you can choose formidable middleware
save the database


the front end uses form-data node to read formdata with multiparty middleware to understand


save the database directly.
if you think you follow the storage path. It's too cumbersome to read the database around
you just use json to store multiple paths


have you ever considered using a third party? For example, Qiniuyun and so on, these manufacturers often have ready-made demo, multi-file multipart upload in minutes, so there is no need to repeat the wheel

.
Menu