• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts2 & XmlHttpRequest & input_file & Html5

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file upload interceptor requires that the request be a multiPartRequest. I have to do this in javascript to make my file upload work when I submitted my form:

document.forms[0].encoding = "multipart/form-data";

However, that might conflict with how an XMLHttpRequest needs to work, so this might not be supported by struts2.
 
fres nillo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom, thanks for your response, I can tell you that the form has a mutipart/form-data in the <form> tag, because if I do a submit it works perfectly. In the example I changed xmlHttpRequest.setRequestHeader("Content-Type","application/octet-stream") by xmlHttpRequest.setRequestHeader("Content-Type","multipart/form-data"), and the result is the same, debugging is viewed that the client don't send all the data before the server finish his action (File and InputStream has a null value and only has been sent the header data when it happends, seeing the traffic). I think that there is a configuration and it's needed to permit XMLHttpRequest with FileUpload Interceptor because the client waits finish the server action and has a response (in my case an HTML with error because the file is empty).
 
fres nillo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solution:

I test it and works for me, the idea was build all the post source code and send it like this:

var BOUNDARY = "myMark------"+(new Date).getMilliseconds();
var source = '';
source += "--" + BOUNDARY + "\r\n" + "Content-Disposition: form-data; name=\"fileNameInAction\"";
source += '; filename="' + file.name + '"' + "\r\n" + 'Content-type: ' + file.type;
source += "\r\n" + "\r\n" + file.getAsBinary() + "\r\n";
req += "--" + BOUNDARY + '--';
httpRequest.open("POST","action.action");
httpRequest.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
httpRequest.sendAsBinary(req);
 
reply
    Bookmark Topic Watch Topic
  • New Topic