File upload with Spring Web Flow

997 Views Asked by At

I have searched and read multiple answers on file upload using Spring Web Flow but all seems to give same result. Hence posting.

public class FileUploadForm {
    private transient MultipartFile file;

    //Additional fields
}

Flow

<view-state id="start" model="fileUploadForm">
    <transition on="submit" to="submit"/>
    <transition on="cancel" to="cancel"/>
</view-state>

<action-state id="submit">
    <evaluate expression="someActions.review(fileUploadForm)" />
    <transition on="success" to="home"/>
</action-state>

JSP

<form:form modelAttribute="fileUploadForm" enctype="multipart/form-data"> <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/> <form:label path="file">Upload File</form:label> <form:input type="file" path="file"/> <button name="_eventId_upload">Upload Button</button> </form:form>

XML

<!--<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
    <!--<property name="maxUploadSize" value="100000"/>-->
<!--</bean>-->

<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="100000" />
</bean>

JAVA

public String review(FileUploadForm fileUploadForm) {
    LOG.info("{}", fileUploadForm.getFile().getContentType());
    return "success";
}

At LOG.info getFile is null. I do get other fields filled in the form, but not the file field.

Can someone please point me in the right direction. Using Webflow 2.5.1

1

There are 1 best solutions below

2
rptmat57 On

Try using a regular input file:

<input type="file" name="file" id="file"/>

[EDIT] to be complete, my working code also uses this form tag:

<form:form modelAttribute="fileUploadForm" action="${flowExecutionUrl}&${_csrf.parameterName}=${_csrf.token}" enctype="multipart/form-data">

and this type of submit button:

<input type="submit" id="upload" name="_eventId_upload" value="Upload"></span>