Dashboard > WebWork > ... > FAQ > How do I handle files upload
  WebWork Log In View a printable version of the current page.  
  How do I handle files upload
Added by tm_jee, last edited by tm_jee on Nov 10, 2005
Labels: 
(None)

Method A
MultipartRequestWrapper multipartRequest = ((MultipartRequestWrapper)ServletActionContext.getRequest())

With multipartRequest, one could access methods such as getFiles(...), getFile(...), getContentType(...), hasErrors(), getErrors() etc to handle the file uploaded.

Method B (Recommended)
Add a 'fileUpload' interceptor to the action. For example, in the following case:

<form name="myForm" enctype="multipart/form-data">
     <input type="file" name="myDoc" value="Browse ..." />
     <input type="submit" />
  </form>

The action class would requires any (or none, but if none what is the point?) of three methods being defined, in order for the interceptor to populate it with uploaded file information

public void setMyDoc(File myDoc) { ...}
public void setMyDocContentType(String contentType) { .... }
public void setMyDocFileName(String filename) { .... }

with these methods, one could do whatever is needed with the uploaded file. If multiple files are uploaded as in following:

<form name="myForm" enctype="multipart/form-data">
      <input type="file" name="myDoc" value="Browse File A ..." />
      <input type="file" name="myDoc" value="Browse File B ..." />
      <input type="file" name="myDoc" value="Browse File C ..." />
      <input type="submit" />
   </form>

The action class needs only make the corresponding method an array, orders followed such that getMyDoc()[0] will have its content type as getMyDoc()[0] and its file name as getMyDoc()[1].

public void setMyDoc(File[] myDocs) { ... }
public void setMyDocContentType(String[] contentTypes) { ... }
public void setMyDocFileName(String[] fileNames) { ... }

Extra Information:
The following properties in webwork.properties affect the file upload.

webwork.multipart.parser (as of WW2.2 its jakarta by default)
webwork.multipart.saveDir (default to javax.servlet.context.tempdir defined by container)
webwork.multipart.maxSize (approximately 2M by default)

@see webwork.properties
@see com.opensymphony.webwork.dispatcher.FilterDispatcher#doFilter(SerlvetRequest, ServletRepsonse, FilterChain)
@see com.opensymphony.webwork.dispatcher.DispatcherUtil#wrapRequest(HttpServletRequest, SerlvetContext)
@see com.opensymphony.webwork.dispatcher.multipart.MultipartRequestWrapper
@see com.opensymphony.webwork.interceptor.FileUploadInterceptor

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators