gpt4 book ai didi

ajax - POST FormData 对象到 Spring MVC

转载 作者:行者123 更新时间:2023-12-01 05:05:54 25 4
gpt4 key购买 nike

所以我有一个用 AngularJS 制作的表单(根本不适合这个项目,但那是那个人使用的),它具有标准字段和"file"类型输入。从我读过的内容来看,FormData 对象是保存表单的好方法,只是我根本无法让它工作。我尝试按照方法here没有成功,我认为问题是我没有正确编写 Controller 。实际的表单有更多的字段和多个文件,但这是我一直试图开始工作的简化测试:

HTML:

<form enctype="multipart/form-data" method="post" name="testform">
<input type="file" name="file" required />
<input type="text" name="name"/>
</form>

JS:
function submit() {
var fd = new FormData(document.getElementById("testform"));
$.ajax({
url: saveUrl,
type: "POST",
data: fd,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (result) {...}
});
}

我已经使用 MultipartHttpServletRequest 尝试了 Controller 的许多变体,一个对表单建模的对象,不同的标题等。没有什么真正接近工作的,但有点像这样:
@RequestMapping(value = "save", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
@ResponseBody
public String saveShipmentRequest(TestFormData testFormData) {
...
}

如果有人可以提供帮助,那就太棒了。我也愿意使用其他方法来做到这一点,我几乎不知道 Angular 并且对 Spring 相当陌生,所以也许我一起采取了错误的方法......

最佳答案

尝试这个

@RequestMapping(value = "/addProfilePicForPage", method = RequestMethod.POST)
public String methodName(MultipartHttpServletRequest request, @RequestParam String pageid) {
...
}

关于ajax - POST FormData 对象到 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815875/

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