Node js Multer file upload with extra text input fields

半城伤御伤魂 提交于 2019-12-11 17:59:08

问题


I am trying to upload a file to disk using multer. Here is my code:

 const multer  = require('multer');


var storage = multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, __basedir + '/uploads/')
    },
    filename: (req, file, cb) => {
      cb(null, file.fieldname + "-" + Date.now() + "-" + file.originalname)
    }
});


var upload = multer({storage: storage});

And here goes my form to collect the file and some extra input fields

<form  method="post" enctype="multipart/form-data" action="/uploadfile">
<input name="cate" type="hidden" value="<%= category %>" id="cate" name="cate"></input>
<br>
<input type="file" name="uploadfile"  class="btn-success" value="Select Source">
        <input  type="submit" class="btn-success"  ><i class="fas fa-plus"></i> Add a new Source</input>

This function uploads public/img/bg.jpg to my database, thou i would like the file to be picked by the user. How can i get the filepath string in req.files object

fs.createReadStream('public/img/bg.jpg')
  .pipe(fileUpload.createWriteStream())
  .on('error', function(err) {
console.log("fail");})
  .on('finish', function() {
    console.log("success");
  });
  });

来源:https://stackoverflow.com/questions/54530352/node-js-multer-file-upload-with-extra-text-input-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!