0

I would like to display uploaded detail into a list, I preferred to go with any easy step in order to display all the detail. Following is my code that is only able to display one data for each time I uploaded a file. I also understand that I should make the "addObject" in the get method, not post method. How can I display in arrayList or any other way? Any help would be appreciated!

This is controller 
@RequestMapping(value = "/product", method = RequestMethod.GET)
    public Object uploadProducts(@ModelAttribute UploadCreate uploadCreate,HttpSession session,RedirectAttributes redirectAttributes) {

     //   redirectAttributes.addFlashAttribute("list",employeeDetail.getName());
     //   redirectAttributes.addFlashAttribute("name",employeeDetail.getName());;

        return "product/upload";

    }

    @RequestMapping(value = "/product", method = RequestMethod.POST, consumes = "multipart/form-data")
    public Object uploadProducts(@RequestParam("file") MultipartFile file) {

        try {

            UploadCreate uploadCreate = new UploadCreate();
            uploadCreate.setName(file.getOriginalFilename());
            uploadCreate.setContentType(file.getName());
            uploadCreate.setContent(file.getBytes());
            uploadCreate.setUploadedDate(new Date());
            uploadService.uploadProducts(uploadCreate);
            return new ModelAndView("product/upload")
                    .addObject("error", "Product upload scheduled.")
                    .addObject("fileList", uploadCreate);


        } catch (Exception e) {
            return new ModelAndView("product/upload").addObject("error", e.getMessage());
        }
    }

HTML:

<table id="uploaded-files">
    <tr>
        <th>File Name</th>
        <th>File Size</th>
        <th>File Type</th>
        <th>Uploaded Date</th>
    </tr>
    {{#fileList}}
    <tr>
        <td>{{name}}</td>
        <td>{{content}}</td>
        <td>{{contentType}}</td>
        <td>{{uploadedDate}}</td>
    </tr>
    {{/fileList}}
</table>
1
  • you want to display all uploaded files per Session in the fileList ?? Commented Nov 20, 2015 at 9:04

1 Answer 1

2

you should use session scope instead of request scope;

@RequestMapping(value = "/product", method = RequestMethod.POST, consumes = "multipart/form-data")
public Object uploadProducts(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

    try {
        Session session = request.getSession();
        UploadCreate uploadCreate = new UploadCreate();
        uploadCreate.setName(file.getOriginalFilename());
        uploadCreate.setContentType(file.getName());
        uploadCreate.setContent(file.getBytes());
        uploadCreate.setUploadedDate(new Date());
        uploadService.uploadProducts(uploadCreate);
        List<UploadCreate> fileList =     
        (List<UploadCreate>)session.getAttribute("list");
        if(fileList==null){
          fileList = new ArrayList<UploadCreate>();
        }
        fileList.add(uploadCreate);
        session.setAttribute("list",fileList);

        return new ModelAndView("product/upload")
                .addObject("error", "Product upload scheduled.");
         //the method addObject() means to add data into request ; 
         //and the previous request and current request can not share the same data ;


    } catch (Exception e) {
        return new ModelAndView("product/upload").addObject("error", e.getMessage());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.