[英]react + spring boot upload file and form data
我的表格中有一个案例(前端),我可以填写个人数据(姓名、地址、出生日期),然后我可以附加多个图像。
在我的 spring 启动 controller 中:
@RequestMapping(value = "/addCustOrder", method = RequestMethod.POST, consumes = {"multipart/form-data"})
public String CustomerOrder(@ModelAttribute CustOrderRequest coReq, HttpServletRequest request) {
System.out.println("debug ************** ");
System.out.println("ReceiverName :: " + coReq.getReceiverName());
System.out.println("attachmentFile :: " + coReq.getFileAttachment().length);
}
我的 model 包装:
public class CustOrderRequest {
private String receiverName;
private String receiverPhone;
private String itemDescription;
private MultipartFile[] fileAttachment;
}
//setter & getter
前端(反应)代码:
const payload = JSON.stringify({
id: values.id,
receiverName: values.receiverName,
receiverPhone: values.receiverPhone,
itemDescription: values.itemDescription,
fileAttachment: values.fileAttachment
});
axios.post(urlApi, payload)
.then(r => {
// success request
});
在上面的例子中,我总是遇到错误。 像:java.io.IOException:Stream 关闭且附件长度为零/附件大小为零(从 MultipartFile 数组或 MultipartFile 列表切换)。 请对此案例有所了解,因为很多教程仅用于上传附件部分,不包括用户填写的表单数据。 之前谢谢。
更新的前端代码:
let fd = new FormData();
fd.append("fileAttachment", values.fileAttachment);
fd.append("receiverName", values.receiverName);
axios.post(urlApi, fd)
.then(r => {
// success request
});
使用 formdata 更改了前端代码,然后在后端出错:
2020-02-07T17:36:10.231+0700 WARN Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'custOrderRequest' on field 'fileAttachment': rejected value [[object FileList]]; codes [typeMismatch.custOrderRequest.fileAttachment,typeMismatch.fileAttachment,typeMismatch.[Lorg.springframework.web.multipart.MultipartFile;,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [custOrderRequest.fileAttachment,fileAttachment]; arguments []; default message [fileAttachment]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile[]' for property 'fileAttachment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile' for property 'fileAttachment[0]': no matching editors or conversion strategy found]]
你不能 JSON 字符串化文件。 据我所知,为了通过 http 上传文件,您必须将数据作为表单数据发布。 此链接的答案显示了如何在 axios 中发布表单数据。 您的 java 端可能已经配置为表单数据输入。
您正在服务器端使用multipart/form-data
,因此您必须将数据作为formData
发送。
使用const formData = new FormData(form);
而不是JSON.stringify
你的第二个例外是绑定错误,你试图将String
绑定到Multipart
,这是因为这一行
fd.append("fileAttachment", values.fileAttachment);
1-您可以在文件的表单中设置onChange
,例如onFileChangeHandler
<input type="file" className="form-control" name="file" onChange={this.onFileChangeHandler}/>
2- 在formData
设置上传的文件并发送它(如下面的代码)
onChange
主体可以如下
onFileChangeHandler = (e) => {
e.preventDefault();
this.setState({
selectedFile: e.target.files[0]
});
const formData = new FormData();
formData.append('file', this.state.selectedFile);
//Append the rest data then send
axios({
method: 'post',
url: 'myurl',
data: formData,
headers: {'Content-Type': 'multipart/form-data' }
})
.then(function (response) {
//handle success
console.log(response);
},
function(error) {
// handle error
});
以下链接可能对您的情况有用:
如果您使用 html+js 作为前端。 当不需要文件时,以下可能有帮助:
var formData = new FormData(document.getElementById("yourFormId"));
我遇到这个问题是因为 react 中的表单实现错误。
我试图一次附加所有图像。
formData.append("images", images)
循环图像后,它解决了我的问题
for (let i = 0 ; i < images.length ; i++) {
formData.append("images", images[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.