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();
}
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.