Nilam Patil - Naik
Nilam Patil - Naik

Reputation: 75

Send Mulitpart file from Controller to JSP (Spring)

My requirement is to upload a JPEG/PDF file & save it as BLOB. We have done it. But if server side error occurs which redirects to JSP page, I will get all data (e.g. input fields, drop downs, checkbox, etc.) except file. Then again I need to choose a file. Is there any way to preserve a file or send a file from controller to JSP.

Upvotes: 0

Views: 887

Answers (2)

Nilam Patil - Naik
Nilam Patil - Naik

Reputation: 75

    MultipartFile inputFile = fileUploadBean.getFile();
    HttpSession session = request.getSession();
    if(!(inputFile.isEmpty())) {
        session.setAttribute("inputFile", inputFile);
    }
    logger.info("inputFile : " + session.getAttribute("inputFile"));
    if(inputFile.isEmpty() && session.getAttribute("inputFile")!=null) {
        inputFile = (MultipartFile)session.getAttribute("inputFile");
    }

This is what I did.

Upvotes: 0

Didier L
Didier L

Reputation: 20579

No it is not possible.

The simplest workaround is to keep the uploaded file in session so that you can recover it during the next form submission. Take care to users working with several tabs/windows: use a session key that clearly identifies the form on which the user is working. You could for example generate a unique identifier that you then store in a hidden field of the form.

To be able to download it again, you would need to provide second mapping that retrieves the file from the session.

Upvotes: 1

Related Questions