我想提交一个包含多个图像的表单,但是我不能在表单数据中附加文件格式的图像数组。我已经创建了一个产品输入的反应式表单,将采取一些产品信息和文件格式的多个图像。我从表单中获得了所有值,但无法像在.so方法中那样将图像数组附加到图像键提交方法中
onSubmit() {
let itemData = this.itemEntryForm.value;
const formData = new FormData();
for (const propertyKey of Object.keys(itemData)) {
if (propertyKey != 'image') {
formData.append(propertyKey, itemData[propertyKey]);
} else if (propertyKey == 'image') {
for (let i = 0; i < itemData[propertyKey].length; i++) {
formData.append('image', itemData['image'][i]);
}
}
}
//for console formdata
formData.forEach((value, key) => {
console.log(key + '--' + value);
});
}
}
我需要将图像数组存储在图像键中。stackblitz Code Example
发布于 2021-11-05 23:29:03
在else if代码块中,我们将一个变量初始化为一个空数组来存储所有图像,以便在循环完成后将其附加到image
中。但是,我不认为图像数组可以附加到formData,因此JSON.stringify()
else if (propertyKey == 'image') {
const imageList = [];
for (let i = 0; i < itemData[propertyKey].length; i++) {
imageList.push(itemData['image'][i]);
}
formData.append('image', JSON.stringify(imageList));
}
https://stackoverflow.com/questions/69861955
复制相似问题