Don't refresh page after uploading a file using a spring form.

502 views Asked by At

I'm using a spring form to upload a document. The html form...

<form method="post" action="/SafeSiteLive/formTask3.do" enctype="multipart/form-data">
    <table id="documentDetailsTable">
      <tr>
         <td>Document Type: </td>
         <td><select id="documentType" name="type"> </select></td>
      </tr>
      <tr>
         <td>
            Document Name:
         </td>
         <td>
             <input type="text" id="documentName" name="name"/>
         </td>
      </tr>
      <tr id="newFile">
         <td>
            Choose a file:
         </td>
         <td>
            <input type="file" name="file" />
         </td>
      </table>
      <input type="text" style="display: none;" name="taskInstanceId" id="taskInstanceId">

       <input id="uploadButton" value="Upload" type="submit"/>
       <input class="closeButton" id="closeNew" value="Close" type="button"/>
 </form>

This connects to my controller...

    @RequestMapping(value = "/formTask3.do", method = RequestMethod.POST)
public ModelAndView handleFormTaskUpload3(@RequestParam("name") String name,
        @RequestParam("type") String type,
        @RequestParam("file") MultipartFile file,
        @RequestParam("taskInstanceId") int taskInstanceId) {
    System.out.println("handleFormUploadTask.1 ");
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            Document document = new Document();
            document.setBytes(bytes);
            String extension = "";

            int i = file.getOriginalFilename().lastIndexOf('.');
            if (i > 0) {
                extension = file.getOriginalFilename().substring(i + 1);
            }
            document.setExtension(extension);
            document.setName(name);

            document.setType(DocType.documentType.valueOf(type));

            Site site = SiteService.getSite(1);
            document.setSite(site);

            DocumentService.addDocument(document);

            DocumentTaskLink docTaskLink = new DocumentTaskLink();
            DocumentTaskKey docTaskKey = new DocumentTaskKey();
            TaskInstance taskInstance = TaskInstanceService.getTaskInstance(taskInstanceId);

            docTaskKey.setDocument(document);
            docTaskKey.setTaskInstance(taskInstance);
            docTaskLink.setKey(docTaskKey);

            DocumentService.saveNewDocumentTaskLink(docTaskLink);

            if (bytes != null) {
                System.out.println("handleFormUpload. File uploaded with bytes size = " + bytes.length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return new ModelAndView("I dont want a new Model And View");
    }
    return new ModelAndView("redirect:uploadFailure");
}

The problem is this form returns a modelAndView which redirects the page.

I would like this to ultimately return a string saying 'success' or 'failure' in which I can respond by informing the user of this success with a dialog (without refreshing the page). I don't mind even just having this controller a void, mostly I just don't want to have to reload the page whenever a file is uploaded.

Is this possible?

How come when I make my controller a void the page still redirects to "/SafeSiteLive/formTask3.do"?

Can I return other things using this form upload controller?

0

There are 0 answers