Download XLS file via POST to Spring MVC

孤街浪徒 提交于 2020-01-05 03:26:48

问题


Please I am annoyed with a problem. I need to download a xls created on the fly on the Java.

This is my client-side code:

exportToExcel: function(filters, exportData) {
        $.ajax
        ({
            type: "POST",
            url: 'status/exportToExcel',
            async: true,
            data: {columns: exportData, filters: JSON.stringify(filters)},
            success: function (data) {
            var blob = new Blob([data], { "type" : "application/vnd.ms-excel" });
                var objectUrl = URL.createObjectURL(blob);
                window.open(URL.createObjectURL(objectUrl));
        },
        error: function (a,b,c){
        alert(c);
        }
    });
}

And this is my serve-side code:

@ResponseBody
@RequestMapping(value = "exportToExcel")
public void exportToExcel(
        @RequestParam(value = "columns", required = true) List<String> columns,
        @RequestParam(value = "filters", required = false) String filters, HttpServletRequest request){

    request.getHeaders().setContentType(getMediaType(excelFile));
    request.getHeaders().set("Content-Disposition", String.format("attachment; filename=%s.%s", excelFile.getName(), excelFile.getFormat()));

    try {
        FileCopyUtils.copy(new DataInputStream(new FileInputStream(excelFile.getFile())), httpOutputMessage.getBody());
    } catch (IOException e) {
        logger.error("Exception in file download", e);
    }catch(Exception e){
        logger.error("Exception in file download", e);
    }
}

I don't know how to show the download dialog, if it was GET a simple window.location solve the problem but my parameters can be so big that GET could not resolve.


回答1:


@RequestMapping(value = "/exportToExcel", method = RequestMethod.POST)
@ResponseBody
public void exportToExcel(-, -, -, -, HttpServletRequest request,HttpServletResponse response )
{
    try{
    InputStream inputStream = service.calltoGetInputStream(); //logic to get inputstream
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", "excelfilename.xlsx");
    response.setHeader(headerKey, headerValue);

    try {
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    }catch(Exception e){
        System.out.println("Exception in file download :"+e);
    }
}



回答2:


I resolved my problem doing a POST, I saved the file in a folder and after that I returned the path to my javascript client. With the filena was simple do a GET request.]

exportToExcel: function(filters, exportData) {
        $.ajax
        ({
            type: "POST",
            url: 'status/exportToExcel',
            async: true,
            data: {columns: exportData, filters: JSON.stringify(filters)},
            success: function (data) {
                window.location = 'status/downloadExcel?file='+data;
            },
        });
    }


来源:https://stackoverflow.com/questions/23479563/download-xls-file-via-post-to-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!