Monday, 12 August 2013

Cannot upload file to struts method while using "dojox.form.Uploader"

Cannot upload file to struts method while using "dojox.form.Uploader"

I would like to use dojo.xhrPost to post form data and file, however, I
can only get the textfield value back but without file, here is my code:
Javascript:
dojo.ready(function(){
dojo.connect(dojo.byId('btnSaveImgRecord'), 'onclick', function(event){
var xhrArgs = {
form: dojo.byId("frmImgRecord"),
handleAs: "json",
mimetype: "text/html",
load: function(data){
// do stuff...
},
error: function(error){
// do stuff...
}
}
// Call the asynchronous xhrPost
var deferred = dojo.xhrPost(xhrArgs);
});
});
Jsp mark up:
<s:form data-dojo-type="dijit/form/Form" action="saveImg" method="POST"
enctype="multipart/form-data" theme="simple" id="frmImgRecord"
name="frmImgRecord">
<s:token/>
<input name="filePlan" multiple="true" type="file"
data-dojo-type="dojox.form.Uploader" label="Select Some Files"
id="filePlan" />
<br/>File Name: <s:textfield name="txtFileName" id="txtFileName"
data-dojo-type="dijit/form/ValidationTextBox"/>
<button data-dojo-type="dijit/form/Button" type="button"
id="btnSaveImgRecord">
Update IMG Record
</button>
</s:form>
Action Method:
private File filePlan;
private String txtFileName;
public String getTxtFileName() {
return txtFileName;
}
public void setTxtFileName(String txtFileName) {
this.txtFileName = txtFileName;
}
public File getFilePlan() {
return filePlan;
}
public void setFilePlan(File filePlan) {
this.filePlan = filePlan;
}
public String saveImg() throws Exception{
System.out.println("saveImg");
Enumeration<String> parameterLists =
ServletActionContext.getRequest().getParameterNames();
while(parameterLists.hasMoreElements()) {
System.out.println("Q: " + parameterLists.nextElement().toString());
}
System.out.println(filePlan == null);
System.out.println("txtFileName: " + txtFileName);
if(filePlan != null){
System.out.println(filePlan.getName());
System.out.println(new
MimetypesFileTypeMap().getContentType(filePlan));
}
return StrutActionConstant.STATUS_SUCCESS;
}
The debug log:
saveImg
Q: txtFileName
Q: token
Q: struts.token.name
true
txtFileName: Hello Test!
Please help me out if I have done something wrong, thank you!

No comments:

Post a Comment