通过Angular $ .http POST下载文件

时间:2016-07-06 19:22:35

标签: javascript java angularjs

我正在尝试下载我的服务器在我的UI中生成的压缩文件。我不知道如何获取文件下载。我们设置它,以便我们可以使用window.open下载我们传递URL的地方并打开一个空白页面。我们现在需要在它有一个机构的情况下进行POST。我还没有看到一种方法将它与window.open一起发送。有没有人对如何访问返回的文件有任何指示?

这是我目前的代码......

@RequestMapping(method = RequestMethod.POST, value = "/archives/download", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Integer> getArchive(HttpServletResponse response, @RequestBody List<GeneratedReport> reportList) {
    System.out.println(reportList.get(0).getFileLocation());
    List<String> filesToDownload = new ArrayList<>();
    reportList.stream().forEach(e -> filesToDownload.add(e.getFileLocation()));
    filesToDownloadAndZip(response, filesToDownload, "zipped_file.zip");
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=zipped_file.zip");
    return new ResponseEntity<Integer>(200, HttpStatus.OK);

}

private void filesToDownloadAndZip(HttpServletResponse response, List<String> filesToDownload, String archiveFileName) {
    try {
        ByteArrayOutputStream baos = FileIO.CreateArchive(filesToDownload);

        if (baos != null && baos.size() > 0) {
            // Set the content type and attachment header.
            response.addHeader("Content-disposition", "attachment;filename=" + archiveFileName);
            response.setContentType("application/zip");
            response.setContentLength(baos.size());
            baos.writeTo(response.getOutputStream());
            response.flushBuffer();
        } else {
            LOG.debug("File was null or size 0, try again");
        }
    } catch(Exception ex)
    {
        LOG.debug(ex.getMessage());
    }
}

我拥有的是......

$http.post('api/archives/download', $scope.downloadItems)
                .success(function(data, status, headers, config) {

                    //I dont know what to do here..... :(

                })

0 个答案:

没有答案