Hi All,
I have been looking into the examples for uploading files using
struts and tried many ways to make it work on RAD 6.0 dev environment (WebSphere 6). For some reason the type="file" form field is not getting posted to the Action. I am using working examples from other posters (thanks). Still cannot figure out why the file is being stripped out.
I have added new entries into my struts-config-ext.xml (Stores/WebContent/WEB-INF):
...
<form-beans>
<form-bean name="myForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="myFile" type="org.apache.struts.upload.FormFile"/>
</form-bean>
</form-beans>
...
Note that I am using DynaActionForm.
...
Here is the Action I defined:
<action
path="/FileUpload"
type="com.nsp.utils.StrutsUploadAction"
name="myForm"
scope="request"
validate="true"
input="/NSP/nsp_nspfileupload.jsp">
<forward name="success" path="/NSP/nsp_nspfileupload.jsp"/>
</action>
...
The Action I use is here. The issue as well. myFile comes as null all the time.
package com.nsp.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
/**
*
*/
public class StrutsUploadAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
DynaActionForm myForm = (DynaActionForm)form;
if (myForm!=null) {
// Process the FormFile
FormFile myFile = (FormFile)myForm.get("myFile");
// !!! myFile is always null!!!
if (myFile!=null) {
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);
}
}
return mapping.findForward("success");
}
}
And here is the
JSP I am using to get the file:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<head>
<title>File Upload</title>
<html:base />
</head>
<body bgcolor="white">
<html:form action="FileUpload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors /></font>
</tr>
<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="myFile" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
Only Name Value Pair I am getting is the "submit" button.
What am I missing here?
Any help would be greatly appreciated.