Spring - download file and redirect

前端 未结 6 1787
渐次进展
渐次进展 2020-12-31 20:15

I have a download link on my page which works just fine but it doesn\'t refresh/redirects my page. Here\'s my code.

@RequestMapping(method = RequestMethod.PO         


        
6条回答
  •  情歌与酒
    2020-12-31 20:24

    I used the following structure to solve my problem. The function submit the form and back, in other words, you download the file and refresh the previous link. Using this solution, you can even show and hide messages errors with some template render, in my case, I used Thymeleaf.

    To make the code more readable, I removed the Thymeleaf tags.

    JS file:

    function submitAndBack(formId) {
        let formDoc = document.getElementById(formId);
        formDoc.submit();
        sleep(1000).then(function() {
            window.history.back();
        });
    }
    
    function sleep(milliseconds) {
        return new Promise(function (resolve) {
            setTimeout(resolve, milliseconds);
        });
    }
    

    HTML form:

提交回复
热议问题