express js处理动态创建的不同字段中的多个文件上载

dy2hfwbg  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(133)

我试图构建一个动态表单,允许用户动态创建多个文件上载控件,然后将其上载到express js服务器。
我使用jquery创建了表单,以允许动态文件上载控件
我的问题是如何处理这个服务器端,因为multer需要先定义文件上传字段。
HTML表单

<form action="/uploadFiles" method="POST" enctype="multipart/form-data">
<div id='Uploadcontainer'>
   <input type='file' name='uploadfiles[]' class='uploadfile' />
</div>
<button id='extraUpload'>Add another field</button>

<input type = "submit"/>
</form>
<script type='text/javascript'>
  $('#extraUpload').click(function(){
      $('.uploadfile:last').clone().appendTo('#uploadContainer');
  });
</script>

app.js

let express = require('express');
const multer = require("multer");

let app = express();

var storage = multer.diskStorage(
{
    destination: function (req, file, callback) 
    {
        var fullPath = path.join(__dirname, 'uploads');
        callback(null, fullPath);
    },
    filename: function (req, file, callback) 
    {
        callback(null, file.originalname);
    }
});

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

app.post('/uploadFiles', upload.fields(
[
   /*
      Normally upload fields are defined here but I am not sure how to define it with dynamic upload controls. 

Example of field
{
   name: "uploadField", maxCount:1
} 
   */
]), 
function(req, res, next)
{
  // ...
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题