簡體   English   中英

將從服務器收到的Excel文件保存在客戶端計算機上

[英]Save excel file received from the server on client machine

我有一個鏈接(“ a”元素),該鏈接將發布請求發送到服務器。 服務器響應一個我要在客戶端下載的excel文件。

服務器代碼:

@RequestMapping(value="/applicabilityExcel", method=POST)
@ResponseBody
public void getApplicability(@RequestParam("id") String reportId, HttpServletResponse response, HttpServletRequest request) throws IOException{
    int id = Integer.valueOf(reportId);
    Report report = repository.getReport(id);
    InputStream is = new ExcelWriter().getConformityMatrix(report);
    response.setContentType("application/vnd.ms-excel");
    org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();     
}

客戶代碼:

<a class="appMatrix" href="<c:out value="${report.id}" />"> App</a>
$(".appMatrix").click(function(e) {
    e.preventDefault();
    $.post( "/applicabilityExcel",
            {id:$(this).attr("href")},
            function(data){
                console.log(data);
                //I have the file in data variable and I need now to stock it in the client machine (open dialog window, save it directly...)
            });
});

我的問題是我不知道該怎么做才能在客戶端計算機上存儲此文件(“下載”)。

我試圖將文件下載為text / base64,將其放在“ a”元素的href上並調用click(),但對我不起作用。

歡迎所有回應和建議。

也許你可以在這里嘗試一下,為我工作

$scope.exportExcel = function(){
    var self = this;
    var url = //yourapi;
    $http.get(url, { responseType: "arraybuffer" }).then(
        function (result) {
            var blob = new Blob([result.data], { type: 'application/vnd.ms-excel' });
            var a = document.createElement('a');
            var url = URL.createObjectURL(blob);
            a.href = url;
            a.download = ""//yourfilename
            a.target = '_blank';
            document.body.appendChild(a);
            a.click();;
        });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM