gpt4 book ai didi

javascript - 在没有表单的情况下从ajax向spring Controller 发送多部分请求?

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

我正在从桌面上传图像,并将该图像转换为 JavaScript 中的基本代码。之后,我想通过多部分请求将此图像基本代码发送到 spring Controller 。但我没有使用表单。

HTML

 <input id="inputFileToLoad" type="file"  onchange="encodeImageFileAsURL()">   

JAVA脚本

 window.photoCakeUrl = '<c:url value="/media/image/upload"/>';
function encodeImageFileAsURL() {
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0) {
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
var newImage = document.createElement('img');
var photoCake = srcData;
newImage.src = srcData;
document.getElementById("imgTest").innerHTML = newImage.outerHTML;
var ajax1 = $.ajax({
type: 'POST',
url: photoCakeUrl,
processData: false, // important
contentType: false, // important
dataType: 'json',
data: {photoCak: photoCake}
});

});

},
fileReader.readAsDataURL(fileToLoad);
}
}

Spring Controller :

@RequestMapping(value = "/media/image/upload", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> productPictureUploadnew(MultipartHttpServletRequest request, HttpServletResponse response) {
Map<String, String> resp = new HashMap<>();
String photoCake = request.getParameter("photoCak");


System.out.println("photoCake " + photoCake);

return resp;
}

但是当我生成 AJAX 调用时,会出现 500 错误。如果我只是使用

public Map<String, String> productPictureUploadnew(HttpServletRequest
request, HttpServletResponse response)

然后就可以了。意思是当我使用 MultipartHttpServletRequest 的地方 HttpServletRequest 请求那么它就不起作用。

最佳答案

我得到了解决方案,我们可以在javascript中使用formData,而无需在任何JSP中使用form来发送MultipartHttpServletRequest。

 window.photoCakeUrl = '<c:url value="/media/image/upload"/>';
function encodeImageFileAsURL() {
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0) {
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
var srcData = fileLoadedEvent.target.result; // <--- data: base64
var newImage = document.createElement('img');
var photoCake = srcData;
newImage.src = srcData;
document.getElementById("imgTest").innerHTML = newImage.outerHTML;

var formData = new FormData();
formData.append("imgFile", document.getElementById("inputFileToLoad").files[0]);

var ajax1 = $.ajax({
type: 'POST',
url: photoCakeUrl,
dataType: 'json',
data: {photoCak: photoCake}
});

});

},
fileReader.readAsDataURL(fileToLoad);
}
}

var formData = new FormData();
formData.append("imgFile", document.getElementById("inputFileToLoad").files[0]);

Controller :

 @RequestMapping(value = "/media/image/upload", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> productPictureUploadnew(MultipartHttpServletRequest request, HttpServletResponse response) {
Map<String, String> resp = new HashMap<>();
System.out.println("fsasasafsafsafsafsa");
Iterator<String> itr = request.getFileNames();

String photoCake = request.getParameter("photoCak");
File file;
----------
-------
----------

return resp;
}

谢谢您,希望这对你们有帮助。

关于javascript - 在没有表单的情况下从ajax向spring Controller 发送多部分请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197867/

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