[英]How can I pass my csv file as Form Data using REST API
I'm having a hard time on encoding my uploaded CSV file to Form Data, This is the idea I came up with to be able to pass the actual file to be processed on the backend using post method of my API.我很难将上传的 CSV 文件编码为表单数据,这是我想出的想法,以便能够使用我的 API 的 post 方法在后端传递要处理的实际文件。 But unfortunately, I'm getting an error "TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'."
但不幸的是,我收到错误“TypeError:无法构造'FormData':参数1不是'HTMLFormElement'类型。” .
. I am new to programming so I hope you understand.
我是编程新手,希望你能理解。 Thank you!
谢谢!
Here is some snippet of what I've done:这是我所做的一些片段:
uploadedProducts contains the csv file I uploaded. uploadProducts包含我上传的 csv 文件。
upload() {
let data = new FormData(self.uploadedProducts);
axios.post(`${api}/mc/upload/specifications`, {
data,
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then((result) => {
let responseData = result.data
alert("Uploaded successfully!")
})
.catch((error) => {
self.showErrorMessage(error.message);
})
}
var formData = new FormData(form)
The form
must be an HTML element. form
必须是 HTML 元素。 Please read FormData() .请阅读FormData() 。 From your errors I guess your
self.uploadedProducts
is not a form element.根据您的错误,我猜您的
self.uploadedProducts
不是表单元素。
The alternative is use append
method.另一种方法是使用
append
方法。 Please read FormData.append() .请阅读FormData.append() 。
So your code should be something like:所以你的代码应该是这样的:
upload() {
let data = new FormData();
data.append('fieldName', self.uploadedProducts);
...
// you can omit multipart/form-data header
axios.post(`${api}/mc/upload/specifications`, data)
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.