Javascript 通过Angular$.http POST下载文件
我正在尝试下载服务器在UI中生成的压缩文件。但是我不知道如何下载这个文件。我们有它的设置,以便我们可以下载与窗口。打开,我们传递的url,它会打开一个空白页。我们需要做一个有尸体的帖子。我还没找到一个办法把它和窗户一起寄出去。有人对我如何访问返回的文件有什么建议吗 这是我当前的代码Javascript 通过Angular$.http POST下载文件,javascript,java,angularjs,Javascript,Java,Angularjs,我正在尝试下载服务器在UI中生成的压缩文件。但是我不知道如何下载这个文件。我们有它的设置,以便我们可以下载与窗口。打开,我们传递的url,它会打开一个空白页。我们需要做一个有尸体的帖子。我还没找到一个办法把它和窗户一起寄出去。有人对我如何访问返回的文件有什么建议吗 这是我当前的代码 @RequestMapping(method = RequestMethod.POST, value = "/archives/download", consumes = MediaType.APPLICATION_
@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());
}
}
我认为如果您将数据返回到一个新窗口,那么这会很容易。在控制器中,返回包含文件数据内容的HttpResponseMessage。在js中,您可以执行如下操作:window.open('api/archives/download'+yourSerializedData,'u blank')@李嘉图这就是我们一直在做的,但是我们不能再在url中添加“数据”。我们需要在请求的正文中发送它。我认为如果您将数据返回到一个新窗口,这会很容易。在控制器中,返回包含文件数据内容的HttpResponseMessage。在js中,您可以执行如下操作:window.open('api/archives/download'+yourSerializedData,'u blank')@李嘉图这就是我们一直在做的,但是我们不能再在url中添加“数据”。我们需要在请求的正文中发送它。
$http.post('api/archives/download', $scope.downloadItems)
.success(function(data, status, headers, config) {
//I dont know what to do here..... :(
})