Node.js and Multer - Handle the destination of the uploaded file in callback function (req,res)

前端 未结 3 1150
孤城傲影
孤城傲影 2021-02-04 19:05

I\'m new to Node.js and I ran into a simple problem lately.

I\'m using multer module to upload images. In my web app all the users have a unique id, and I wa

3条回答
  •  醉梦人生
    2021-02-04 19:50

    you can solve this just referring to the input name and rename the path before assigning it.

    app.use(multer({ dest: './standard_folder/',
        rename: function (fieldname, filename) {
    
          var pathHelper ='';
          if(fieldname =='otherKindOfFolderNeeded'){
            pathHelper = '../../path/to/other/folder/';
          }
          return pathHelper+uuid.v4()+Date.now();
        },
        onFileUploadStart: function (file) {
          console.log(file.originalname + ' is starting ...')
        },
        onFileUploadComplete: function (file) {
          console.log(file.fieldname + ' uploaded to  ' + file.path)
          done=true;
        }
      })
    );
    

提交回复
热议问题