PDA

View Full Version : fileupload help



abstraction
Jun 3rd, 2005, 11:48 AM
I'm looking at the fileupload example and I'm having a little trouble figuring out how to process the file from within ProcessUploadAction.java. Can someone give me a brief example of how to get at the file from within ProcessUploadAction.java?

Thanks for the pointers.

klr8
Jun 3rd, 2005, 12:01 PM
Since the ProcessUploadAction puts the FileUploadBean in REQUEST scope (the default), you would do something like this:


public Event processFile(RequestContext context) throws Exception {
FileUploadBean fub=(FileUploadBean)context.getRequestScope().getA ttribute("file");
byte[] fileData=fub.getFile();
... //process file data
return success();
}


You would then need to invoke this processFile() method from an action state, something like this:


<action-state id="processFile">
<action bean="upload.process"/>
<transition on="success" to="..."/>
</action-state>

abstraction
Jun 3rd, 2005, 12:41 PM
That's exactly what I've done. The only problem is that fub ends up being null.

abstraction
Jun 3rd, 2005, 01:06 PM
This did the trick.


public Event processFile&#40;RequestContext context&#41; throws Exception &#123;
bindAndValidate&#40;context&#41;;
FileUploadBean fub=&#40;FileUploadBean&#41;context.getRequestScope&#40;&#41;.getA ttribute&#40;"file"&#41;;
byte&#91;&#93; fileData=fub.getFile&#40;&#41;;
... //process file data
return success&#40;&#41;;
&#125;