Spring - download file and redirect

前端 未结 6 1802
渐次进展
渐次进展 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:27

    From LottaLava answer, I got the idea to solve a similar problem.

    In JavaScript, after form submission, I wait for one and a half-second and then reload the page. The waiting time is to time for the backend to download the file (here exportToXML) and return the response and then in JavaScript refresh the page.

    form.submit();
    sleep(1500).then(function() {
         document.location.reload();
    });
    

    Here form.submit() calls the controllers action, in your case exportToXML.

    The sleep function is as follows:

    function sleep(milliseconds) {
        return new Promise(function (resolve) {
            setTimeout(resolve, milliseconds);
        });
    }
    

    The sleep function refers to here

提交回复
热议问题