使用包含输入名称数组的multer上传文件

时间:2016-10-24 09:58:40

标签: node.js pug multer

我正在使用multer nodejs上传文件。一切正常,但输入类型="文件"包含name ="保修[] [warantycopy]"我无法访问此文件。 下面是我在index.js中的代码:

var multer  = require('multer');
var invoice_copies = '';
var storage_Copies = multer.diskStorage({

  destination: function (req, file, cb) {
    // console.log("fileefefef");
    // console.log(file);
    cb(null, 'uploads/'+file.fieldname)
  },
  filename: function (req, file, cb) {
    invoice_copies=(file.originalname).replace(/ /g,"_")
    cb(null, invoice_copies)
  }
});

var uploadCopies = multer({ storage: storage_Copies })

发帖请求包含此代码

router.post('/vehicle-battery',uploadCopies.fields([{name:'invoice_copy1'},
    {name:'warrantyDetails'}]), function(req, res) {

//我的代码 } 我的玉文件是:

 form#formAddUser(name="addVehicle",method="post",action="/vehicle-battery",enctype="multipart/form-data")
        input(type="file", name="invoice_copy1",class="form-control")
        input#warrantyCard(type="text", placeholder="warranty Card Number", name="warrantyDetails[0][warrantyCardNumber]",class="form-control")
        input#warrantyCardExpiry(type="text", placeholder="warranty Card Expiry in Month", name="warrantyDetails[0][warrantyCardExpiry]",class="form-control")
        input#warrantyCardCopy(type="file", placeholder="warranty Card Expiry copy", name="warrantyDetails[0][warrantyCardCopy]",class="form-control")
        textarea(placeholder="Particulars" name="battery_paticulars")
        button#btnSubmit(type="submit",class="form-control") submit

如果我正在使用

input#warrantyCardCopy(type="file", placeholder="warranty Card Expiry copy", name="warrantyDetails",class="form-control")

此代码正常运行。

1 个答案:

答案 0 :(得分:0)

来自上面的评论:

工作了这么长时间后,我解决了它。我正在提供我的解决方案,以便它可以帮助其他人。我用过

router.post('/vehicle-battery',uploadCopies.any(), function(req, res) { //my code }