SWF-1113 - File Upload - Add a section to the documentation showing how to do file upload

This commit is contained in:
Jeremy Grelle
2009-08-06 00:50:33 +00:00
parent ad32048843
commit 2c54458884

View File

@@ -399,6 +399,59 @@ public class PrintBoardingPassAction extends AbstractAction {
In this example, when the print event is raised the flow will call the printBoardingPassAction.
The action will render the PDF then mark the response as complete.
</para>
</sect2>
<sect2 id="file-upload">
<title>Handling File Uploads</title>
<para>
Another common task is to use Web Flow to handle multipart file uploads in combination with Spring MVC's
<code>MultipartResolver</code>. Once the resolver is set up correctly <ulink url="">as described here</ulink> and the submitting
HTML form is configured with <code>enctype="multipart/form-data"</code>, you can easily handle the file upload in a
transition action. Given a form such as:
</para>
<programlisting language="xml"><![CDATA[
<form:form modelAttribute="fileUploadHandler" enctype="multipart/form-data">
Select file: <input type="file" name="file"/>
<input type="submit" name="_eventId_upload" value="Upload" />
</form:form>]]>
</programlisting>
<para>
and a backing object for handling the upload such as:
</para>
<programlisting language="java"><![CDATA[
package org.springframework.webflow.samples.booking;
import org.springframework.web.multipart.MultipartFile;
public class FileUploadHandler {
private transient MultipartFile file;
public void processFile() {
//Do something with the MultipartFile here
}
public void setFile(MultipartFile file) {
this.file = file;
}
}]]>
</programlisting>
<para>
you can process the upload using a transition action as in the following example:
</para>
<programlisting language="xml"><![CDATA[
<view-state id="uploadFile" model="uploadFileHandler">
<var name="fileUploadHandler" class="org.springframework.webflow.samples.booking.FileUploadHandler" />
<transition on="upload" to="finish" >
<evaluate expression="fileUploadHandler.processFile()"/>
</transition>
<transition on="cancel" to="finish" bind="false"/>
</view-state>]]>
</programlisting>
<para>
The <code>MultipartFile</code> will be bound to the <code>FileUploadHandler</code> bean as
part of the normal form binding process so that it will be available to process during the
execution of the transition action.
</para>
</sect2>
</sect1>
</chapter>