Well use Apache File Upload Utils library to make it more easy. For cross domain policy, you have to solve it some server side policies, or use some proxy server like nginx.
FormData with Ajax upload file cross domain ,how can the server get the file?
-
16-07-2023 - |
Question
my domain is localhost:8084,want to upload file to localhost:8086 the js is
var xhr = new XMLHttpRequest();
xhr.open("post", "http://localshot:8086"+ "?type=ajax",true);
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.setRequestHeader("Content-type", "multipart/form-data;");
// 模拟数据
var fd = new FormData();
fd.append("upfile", f); //f is a File object
fd.append("result","tskdskfjsf");
xhr.send(fd);
xhr.addEventListener('load', function(e) {
var r = e.target.response, json;
me.uploadComplete(r);
if (i == fileList.length - 1) {
$(img).remove()
}
});
the server(java) :
System.out.print(getPara("result"));
UploadFile uf = getFile("upfile", path.getAbsolutePath() + "/");
my question :
- i will get no data in console.
- when executed the statement
getFile("upfile",path.getAbsolutePath() + "/")
will throw an exception"java.io.IOException: Posted content type isn't multipart/form-data"
the method getFile is from third framework:
public UploadFile getFile(String parameterName, String saveDirectory) {
getFiles(saveDirectory);
return getFile(parameterName);
}
public List<UploadFile> getFiles(String saveDirectory) {
if (multipartRequest == null) {
multipartRequest = new MultipartRequest(request, saveDirectory);
request = multipartRequest;
}
return multipartRequest.getFiles();
}
expect your help ,thanks!
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow