简体   繁体   English

节点JS中的Multer req.body为空

[英]Multer req.body empty in node js

I am using multer to upload form data( some image files). 我正在使用multer上载表单数据(一些图像文件)。

But req.body and req.files both are coming empty. 但是req.body和req.files都为空。 when I am uploading images I got the successful message " your image has been uploaded" but my folder is empty. 当我上传图像时,收到成功消息“您的图像已上传”,但是我的文件夹为空。

I tried all solution available on StackOverflow as well as GitHub. 我尝试了StackOverflow和GitHub上所有可用的解决方案。 but no luck. 但没有运气。

below my code 在我的代码下面

form in the HTML file 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 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");
        }
    });
});

I tried: 我试过了:

Express, Multer, BodyParser req.body empty array Express,Multer,BodyParser req.body空数组

multer req.body showing empty always multer req.body始终显示为空

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

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

and I followed this tutorial for multer 我按照本教程学习

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

I am unable to understand that what am I doing wrong? 我无法理解我在做什么错?

please help. 请帮忙。

Thanks in advance 提前致谢

First, .ajaxSubmit() is not core jQuery functions. 首先, .ajaxSubmit()不是jQuery的核心功能。 You need the jQuery Form Plugin and I don't know if you import it like below: 您需要jQuery Form插件 ,但我不知道是否按以下方式导入它:

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

The easiest way to do it will be to do it without a plugin like this: 最简单的方法是不用这样的插件:

$('#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;

});

Now in your backend, your destination should better be (with a folder uploads in your public folder): 现在在您的后端,您的目的地最好是(在公共文件夹中上传文件夹):

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

I just changed my storage to 我只是将存储空间更改为

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

And it is workings fine.. 而且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM