How to store image in node backend?

后端 未结 3 804
终归单人心
终归单人心 2021-01-16 06:08

I am using node and express for backend and Mongo DB for storage.I am using multer middleware for storing image,i got a problem and the problem is when i store an image from

3条回答
  •  离开以前
    2021-01-16 06:39

    var dateString = Date.now(); var storage = multer.diskStorage({

    destination: (req, file, cb) => {
    cb(null, './uploads/')
    
    
     },
    
    
    filename: (req, file, cb) => {
    cb(null,  dateString+'_'+file.originalname)
    
    
    }
    });
    
    
    
    var maxSize=3  1000  1000;
    
    
    var upload = multer({ storage: storage,
    limits: { fileSize: maxSize }})
    
    var upload = multer({ storage: storage , limits: { fileSize: maxSize }});
    app.post("/fileupload", upload.single("fileToUpload"), function (req, res) {
    
    
    console.log(req.file.originalname)
    var uploaded =  "http://localhost:4001/"+dateString+'_'+req.file.originalname;
    console.log(uploaded)
    res.json({status: 200,msg:'File saved successfully',data:uploaded});
    });
    

提交回复
热议问题