ajax requests to download Execl table

Execl table is often used to store two bit data. Java can also directly operate Execl table. jxl and poi are often used.

In this project, the poi I used writes data to Execl. At the beginning of the design, the front end sends an ajax request, and the back end responds and then the browser downloads the Execl table.

function exportGjOil(){
            type: 'post',
            url: '/sysUser/selectAuditType',
            data: {
            dataType: 'json',
            success: function (data) {
                if(data==2 ||data==4){
                        type: 'get',
              //To send the request the day after tomorrow, the background writes data to execl and returns it to the front end in the form of stream url:
'/materialGjOilDetail/downloadEcexl', data: { year:[[${year}]], companyId:[[${companyId}]] }, // dataType: 'json', success: function (data) { $.infoMsg("File downloaded to D:\Verification result"); }, error: function (data) { $.infoMsg("Download failed, please download again"); } }); //window.location.href = "/materialGjOilDetail/downloadEcexl?year="+[[${year}]]+"&companyId"+[[${companyId}]]; } else{ $.infoMsg("The company doesn't have fuel bus information"); } } });

The above code is the first design idea, but after each request is returned, the response of the browser console is garbled. This is because the ajax request is only a "character" request, that is, the content of the request is stored in the text type. The return type of the ajax function of JQuery is only xml, text, json, html and other types, without the "flow" type,. The file is downloaded in binary form, so ajax can't handle the response of binary stream to download the file. So we want to achieve ajax download, can not use the corresponding ajax function for file download.

Finally, you can change the Ajax request to be implemented with < a > tags. Or you can use window.location.href="URL". If you have to use ajax, you can use it to generate a form, submit parameters with this form, and return data of "flow" type.

Tags: Javascript JSON Java JQuery xml

Posted on Mon, 02 Dec 2019 14:30:58 -0500 by markhard