[英]How to append more than one array to formData()?
I am using react fro front end and express for backend.我正在使用前端的反应和后端的表达。 Also multer, which is a third party package to handle file inputs.
还有multer,它是一个处理文件输入的第三方包。 Now what I want is that there should be an array of objects that I could append to formData and send it via axios.
现在我想要的是应该有一个对象数组,我可以将其附加到 formData 并通过 axios 发送它。 However I fail to do so.
但是我没有这样做。 I wrote the following code:
我写了以下代码:
const handleSubmit = (id, e) => {
//formdata object to be created
e.preventDefault()
const formData = new FormData;
for (var i = 0; i < dataFolder.length; i++) {
formData.append('body[]', dataFolder[i])
}
for (var i = 0; i < imageArray.length; i++) {
formData.append('image', imageArray[i])
}
addUpdateProduct(formData)
handleClose()
}
addUpdateProduct(formData) is a redux action that then calls the backend route. addUpdateProduct(formData) 是一个 redux 操作,然后调用后端路由。
'DataFolder' here is an array of objects that has key value pairs such as:此处的“DataFolder”是具有键值对的对象数组,例如:
dataFolder = [ {name: 'BB cream', type: 'cream'},{name: 'Neomycin', type: 'ointment'}]
'imageArray'is also an array of images containing an array of files. 'imageArray' 也是一个包含文件数组的图像数组。 When i console it in my express controller i get the following output:
当我在我的快速控制器中对其进行控制台时,我得到以下输出:
The files get uploaded correctly in the files array but dataFolder does not show up the expected way.文件在 files 数组中正确上传,但 dataFolder 未按预期方式显示。 Any solutions?
任何解决方案?
See the description of append :参见append的描述:
The field's value.
字段的值。 This can be a USVString or Blob (including subclasses such as File).
这可以是 USVString 或 Blob(包括子类,例如 File)。 If none of these are specified the value is converted to a string.
如果没有指定这些值,则该值将转换为字符串。
{name: 'BB cream', type: 'cream'}
is an object, not a string or a blob. {name: 'BB cream', type: 'cream'}
是一个对象,不是字符串或 blob。 If you convert a plain object to a string you get '[object Object]'
as you can see in the data received by the server.如果将普通对象转换为字符串,则会得到
'[object Object]'
正如您在服务器接收到的数据中看到的那样。
You could encode it as JSON to make it a string.您可以将其编码为 JSON 以使其成为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.