Multer req.body在节点js中为空

问题描述 投票:1回答:2

我正在使用multer上传表单数据(一些图像文件)。

但是req.body和req.files都是空的。当我上传图片时,我收到了成功的消息“你的图片已被上传”,但我的文件夹是空的。

我尝试了StackOverflow和GitHub上的所有解决方案。但没有运气。

低于我的代码

HTML文件中的表单

<form id="uploadForm" enctype="multipart/form-data" action="multerFile" method="post">
  <input type="file" name="userPhoto" multiple />
  <input type="submit" value="Upload Image" name="submit">
  <input type='text' id='random' name='random'>
  <br>
  <span id="status"></span>
</form>

<script>
  $(document).ready(function () {
    $('#uploadForm').submit(function () {
      $("#status").empty().text("File is uploading...");
      $(this).ajaxSubmit({
        error: function (xhr) {
          status('Error: ' + xhr.status);
        },
        success: function (response) {
          console.log(response)
          $("#status").empty().text(response);
        }
      });
      return false;
    });
  });
</script>

index.js

app.post('/multerFile', function (req, res) {
    console.log("hi i am multer function")
    var storage = multer.diskStorage({
        destination: 'C:/Users/chandra/Documents/project/node_project/public'
    });
    var upload = multer({ storage: storage }).array('userPhoto', 2);

    upload(req, res, function (err) {
        if (err) {
            console.log(err);
            return res.end("Error uploading file.");
        } else {
            console.log(req.body)
            console.log(req.files);
            req.files.forEach(function (f) {
                console.log(f);
                // and move file to final destination...
            });
            res.end("File has been uploaded");
        }
    });
});

我试过了:

Express, Multer, BodyParser req.body empty array

multer req.body showing empty always

https://github.com/expressjs/multer/issues/351

https://github.com/expressjs/multer/issues/391

我按照本教程进行了multer

https://codeforgeek.com/2016/01/multiple-file-upload-node-js/

我无法理解我做错了什么?

请帮忙。

提前致谢

javascript node.js jquery-file-upload multer
2个回答
0
投票

首先,.ajaxSubmit()不是核心jQuery函数。你需要jQuery Form Plugin,我不知道你是否导入它如下:

<script src="http://malsup.github.com/jquery.form.js"></script> 

最简单的方法是在没有这样的插件的情况下做到这一点:

$('#uploadForm').submit(function() {

  $("#status").empty().text("File is uploading...");

  var formData = new FormData($('#uploadForm')[0]);

  $.ajax({
      type: 'POST',
      url: '/multerFile',
      data: formData,
      success: function(response) {
          console.log(response)
          $("#status").empty().text(response);         
      },
      contentType: false, // Prevent jQuery to set the content type
      processData: false  // Prevent jQuery to process the data    
  });

  return false;

});

现在在你的后端,你的目的地应该更好(在你的公共文件夹中上传文件夹):

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
       cb(null, './public/uploads/')
    },
});

0
投票

我刚刚将存储空间更改为

const storage = multer.diskStorage({
       destination: './public/uploads/',
       filename: function(req, file, cb){
         cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
       }
     });

它工作正常..

© www.soinside.com 2019 - 2024. All rights reserved.