Handling errors in file upload with struts

Ramon Gonzalez

Not that I know of, but here is a simple solution using JavaScript

When a file is typed or selected using the "BROWSE" button, as soon as the field is populated, a function populates a hidden input text field with the value of the file name that was just selected. Whe you submit your form, you will have the name of the file as part of your request.getParameters("file1name") in your UploadForm.java.

The problem is that the <INPUT TYPE-"file"> will not take a default value, even if you try to force it thru JavaScript. I think this is probably a security feature in JavaScript to avoid "hidden" file uploads from your PC, but you can display the path they entered so they can cut-n-pate it into the INPUT field.

<INPUT TYPE="file" NAME="file1" SIZE="60" MAXLENGTH="60" onchange='return file1Value(this)'>
<INPUT TYPE="hidden" NAME="file1name" VALUE="">

<SCRIPT LANGUAGE="JavaScript1.2">
function file1Value(thisvalue) {
  thisvalue.form.file1name.value = thisvalue.form.file1.value.toString();
  return true;