Search code examples
ajaxspringspring-mvcdwr

Spring mvc & DWR - Need help to implement progress bar while upload file


I got a classic multipart form with spring mvc 3 which works fine : I can upload a file to a controller with 2 parameters ( name and description). My controller get the MultipartFile file and parameters, and put in database with DAO classes.

My Goal : just add to my form a progress bar while uploading the file !

Can anyone help me by telling me different steps to do. I prefer do it with DWR if possible to expose the ProgressListener method. (implement a ProgressListener for for MultipartResolver ,javascript to add to my form)

Any help Would Be Appreciate!

Here is my form : ( ajoutDocumentRapport.jsp)

<form:form method="post" action="save.html" commandName="documentFormBean" enctype="multipart/form-data">
<input type="hidden" name="depot" value="${depot.id}"/>
<table>
<tr>
    <td><form:label path="name">Name of file</form:label></td>
    <td><form:input path="name" /></td> 
    <td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
    <td><form:label path="description">Description of file</form:label></td>
    <td><form:textarea path="description" /></td>
    <td><form:errors path="description" cssClass="error"/></td>
</tr>
<tr>
    <td><form:label path="content">Document</form:label></td>
    <td><input type="file" name="file" id="file"></input></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="add Document."/>
    </td>
</tr>
</table>    
</form:form>

And here is my controller :

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(HttpServletRequest request,
        @Valid DocumentFormBean documentFormBean,BindingResult result,
        @RequestParam("file") MultipartFile file) {
    Map<String,Object> map = new HashMap<String, Object>();

    map.put("documentFormBean", new DocumentFormBean());
    map.put("documentList",documentDao.findAllForaDepot(Long.parseLong((String) request.getParameter("depot"))));
    map.put("depot", depotDao.find(Long.parseLong((String) request.getParameter("depot"))));

    if (result.hasErrors()) {
        map.put("errors", result);
        return new ModelAndView("ajoutDocumentsRapport", map);
    } else{

    try {
        Blob blob = Hibernate.createBlob(file.getInputStream());
        Document newDocument = new Document();
        newDocument.setTitle(documentFormBean.getName());
        newDocument.setDescription(documentFormBean.getDescription());
        newDocument.setFilename(documentFormBean.getName());
        newDocument.setContentType(file.getContentType());
        newDocument.setFilename(file.getOriginalFilename());
        newDocument.setContent(blob);
        newDocument.setDepot(depotDao.find(Long.parseLong((String) request.getParameter("depot"))));
        documentDao.save(newDocument);
        map.put("documentList",     documentDao.findAllForaDepot(Long.parseLong((String) request.getParameter("depot"))));
        } catch (IOException e) {
        e.printStackTrace();
    }

    return new ModelAndView("ajoutDocumentsRapport",map);
    }
}

Solution

  • Check this solution that I use: Use Spring MVC 3.1 and JQuery to make it work

    http://abambenjamin.blogspot.mx/2012/04/ajax-jquery-html5-progressbar-spring.html