Solve the problem that AJAX does not support downloading files

Application scenario

The front end wants to access an interface for downloading files. If you use ajax to access it, you can see that the status code is 200, which is successful, but the file is not downloaded successfully. No matter if the modification request type is POST, GET, or PUT, or even DELETE, the result is 200, but it just cannot be downloaded.

Cause Analysis

The parameters returned by the ajax request are json, text, html, xml. Ajax cannot call the IO stream, so the download function cannot be completed. Ajax can obtain the data of the file, but it cannot be saved to the disk. In order to ensure computer security, JS cannot be combined with The disk interacts.

Solution

Use this method to call, just write your own interface name.

 function onDownload(bdoc) {
    
    
        var $eleForm = $("<form method='post'></form>");
        $eleForm.attr("action",__rootPath + "/qwq/know/file/preview.do?fileId="+bdoc);
        $(document.body).append($eleForm);
        $eleForm.submit();
    }   

Guess you like

Origin blog.csdn.net/numbbe/article/details/108993637