【问题标题】:upload multiple images from multiple form field or multiple input type in node js从多个表单字段或节点js中的多个输入类型上传多个图像
【发布时间】:2021-08-05 06:11:43
【问题描述】:
<form action="/profile" method="post" enctype="multipart/form-data">
  <input type="file" name="img1" />
  <input type="file" name="img2" />
  <input type="file" name="img3" />
</form>

如何从节点 js 中的不同表单字段上传所有图像

【问题讨论】:

    标签: javascript node.js express file-upload multer


    【解决方案1】:

    这是表格...

    <form action="/profile" method="post" enctype="multipart/form-data">
      <input type="file" name="img1" />
      <input type="file" name="img2" />
      <input type="file" name="img3" />
    </form>
    

    上传文件需要安装multer npm包

    npm i --save multer
    

    在你需要这个包之后

    const multer  = require('multer')
    
    var cpUpload = upload.fields([{ name: 'img1', maxCount: 1 }, { name: 'img2', maxCount: 1 }, { name: 'img3', maxCount: 1 }])
    app.post('/profile', cpUpload, function (req, res, next) {
      // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
      //
      // e.g.
      //  req.files['img1'][0] -> File
      //  req.files['img2'] -> Array
      //
      // req.body will contain the text fields, if there were any
    })
    

    【讨论】:

    • 您还可以为每种文件类型增加多张图片上传的maxCount
    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多