gpt4 book ai didi

node.js - FormData FileList 作为 [Object FileList] 返回

转载 作者:行者123 更新时间:2023-12-05 04:41:26 24 4
gpt4 key购买 nike

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

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

此 FileList 的 Console.log >> enter image description here

但是当我尝试在后端访问同一个 fileList 时,它显示一个空数组...

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

当我这样尝试时:

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

当我将同一个端点与 Postman 一起使用时,效果完美...

最佳答案

刚刚发现发生了什么...我正在传递一个包含 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]);
}
});

关于node.js - FormData FileList 作为 [Object FileList] 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70088172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com