How to upload file using multer or body-parser

后端 未结 4 1945
感动是毒
感动是毒 2021-01-17 12:34

I am a NodeJS beginner, following along a book \"Web Development with MongoDB and NodeJS\". I am stuck at its chapter 6 with \'multer\'. When I use multer for file uploads t

4条回答
  •  误落风尘
    2021-01-17 12:47

    I corrected the code of the book "Web Development with MongoDB and NodeJS" as follows:

    app.use(multer({dest:path.join(__dirname,'../public/upload/temp')}).any());
    .
    .
    .
    .
    const tempPath = req.files[0].path,  // Temporary location of uploaded file
    ext = path.extname(req.files[0].originalname).toLowerCase(), // Get file extension of the uploaded file
    targetPath = path.resolve(`./public/upload/${imgUrl}${ ext}`); // The final path for the image file
    
    
    

    The other parts of code remained intact. It worked and I could upload image files. Best wishes, Mehrdad Sheikhan

提交回复
热议问题