【问题标题】:FormData FileList is returnig as [Object FileList]FormData FileList 返回为 [Object FileList]
【发布时间】:2021-11-23 21:40:24
【问题描述】:

这是一个简单的图片文件列表。

Object.keys(pet).forEach((key) => {
      if (key === "images") {
        formData.append("images", pet[key]); //my fileList
      } else {
        formData.append(key, pet[key]);
      }
    });

此 FileList 的 Console.log >>

但是当我尝试在后端访问同一个文件列表时,它会显示一个空数组...

const images = req.files;
console.log(images); // equals to []

当我这样尝试时:

const images = req.body.images;
console.log(images); // Returns this: [object FileList].

当我将这个端点与 Postman 一起使用时,它的效果非常好......

【问题讨论】:

    标签: node.js reactjs multipartform-data


    【解决方案1】:

    刚刚发现发生了什么...我正在传递一个对象数组,其中包含一个 FileList 是另一个数组,所以它永远不会工作...

     Object.keys(pet).forEach((key) => {
          if (key === "images") {
            let images = Array.from(pet.images[0]); //needed to access the first array first
            for (let i = 0; i < images.length; i++) {
              formData.append("images", images[i]);
            }
          } else {
            formData.append(key, pet[key]);
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多