4

I have a form that I am submitting via jQuery's ajaxSubmit() function. This form includes a file control, and it has been raised as a possible point of failure that if the file selected is renamed, deleted, or otherwise made inaccessible prior to submitting the form, the form may or may not submit based on the browser. While the failure to submit is expected behavior, there is no indicator to the user that the form failed to submit or why, which is being considered a show-stopper. Worse, in IE, the form submits without any file data; while I can catch this on the server side, it would be more ideal if the form did not submit at all.

I am aware that JavaScript is restricted in being able to know anything about the filesystem, and I am similarly aware that there are some not-insignificant issues with anything having to do with cross-browser compatibility. Firefox doesn't seem to throw any kind of exception when the form submission fails, or jQuery is swallowing it. IE toddles along obliviously. I would like to see if there is a way to capture the fact that the form did not submit or submitted with an invalid file. I would also like to capture if there is a problem with the file field prior to submission.

Any ideas?

Roman C
  • 49,761
  • 33
  • 66
  • 176
jzeitler
  • 71
  • 2
  • 2
    This doesn't rise to the level of answer, more like food-for-thought, so I'll post it as a comment. You can do a bit of basic verification at submit time _with recent browsers only_ via the File API (http://www.w3.org/TR/file-upload/). Support for the API is generally good except under IE, where it didn't become available until version 10. – dgvid Apr 15 '13 at 14:04
  • +1, nice question. For general purpose hints on fileUploading with Struts2, feel free to read this too: http://stackoverflow.com/questions/15957470/struts2-sform-element-trims-the-surl-parameter-in-the-action-attribute/15968166#15968166 – Andrea Ligios Apr 15 '13 at 14:55
  • http://stackoverflow.com/questions/10471866/html5-file-api-simple-check-if-file-exists – Andrea Ligios Apr 15 '13 at 14:59
  • I'll look into that; it might solve the problem in Firefox. IE may be able to get away with a server-side failure. – jzeitler Apr 15 '13 at 14:59
  • Through some experimentation, it turns out that IE9 can't even read the .value field of the file input to determine whether or not it exists prior to submission. – jzeitler Apr 15 '13 at 17:29

1 Answers1

0

You can always use the Struts2 validation framework to validate the file object.

Apply fileUpload interceptor to your action that sets the File object to your action when you submit the form.

Once you get the object and it's not null you may proceed with some kind of validations. For example file.exists().

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • That's a good precaution against the file being empty when it's submitted-- the IE behavior-- but in Firefox the form submission is handled as a no-op if the file no longer exists. I need some kind of notification or alert in place of that no-op. – jzeitler Apr 15 '13 at 14:53
  • @jzeitler You may use the client side validation, i.e. when `validate=true` struts will perform validations with javascript or write a custom validator either way if you continue with server side validations and using ajax submit then return some result with notifications. – Roman C Apr 15 '13 at 15:15