posted 13 years ago
Hi all,
I have to said that there are alternatives to this method, but I want to implement this with Struts2 without any new libraries (like ajax-file-upload-struts2), because it works with a simple upload.
I have an action that caught files of a simple form with submit action. It's using in server the FileUpload interceptor and when there is a submit the file go to the server successfully.
Well, the problem happens when it's used an XmlHttpRequest to send this file.
In a summary, here is the code:
var file = document.forms[0].httpFile.files[0]
xmlHttpRequest.open("POST","upload.action",true);
upload = xmlHttpRequest.upload;
upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
console.log((e.loaded/e.total)*100);
}
}, false);
xmlHttpRequest.setRequestHeader("Cache-Control", "no-cache");
xmlHttpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xmlHttpRequest.setRequestHeader("Content-Type","application/octet-stream");
xmlHttpRequest.setRequestHeader("X-File-Name", file.name);
xmlHttpRequest.setRequestHeader("X-File-Size", file.fileSize);
xmlHttpRequest.setRequestHeader('Connection', 'close');
xmlHttpRequest.send(file);
The file is sent to server, but when server launch the action, file is null, I tried with inputstream but i have the same problem. If I submit the form from javascript, it works fine but not with XmlHttpRequest and I don't have any idea why it happens (i think that can be changed the request header and send the full form, but I have changed it and didn't work for me!)
By the way, is possible use the HTML5 feature to see what is the percent uploaded with Struts2 framework in server?
Thanks, and sorry for my English