Angular 不从流中下载文件 ( StreamingResponseBody )
Angular doesn't download a file from a stream ( StreamingResponseBody )
我使用angular下载大文件,后台我使用spring启动,这里是终点代码:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public StreamingResponseBody download(@PathVariable String path) throws IOException {
final InputStream file =azureDataLakeStoreService.readFile(path);
return (os) -> {
readAndWrite(file , os);
};
}
private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
System.out.println("appending to file");
os.write(data, 0, read);
}
os.flush();
}
当我尝试使用 curl 获取文件时它起作用了,我可以看到正在下载的文件并且它的大小在增加:
curl -H "Authorization: Bearer <MyToken>" http://localhost:9001/rest/api/analyses/download --output test.zip
但是,当我尝试使用 angular 下载文件时它不起作用,即使请求成功,我可以在日志中看到文本 "appending to file" 多次显示,但浏览器上没有下载任何内容,这是我的代码:
this.http.get(url, { headers: headers, responseType: 'blob', observe: 'response' })
.subscribe(response => {
const contentDispositionHeader: string = response.headers.get('Content-Disposition');
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], {
type: 'application/zip'
});
saveAs(blob, filename);
});
saveAs() 属于 file-saver,顺便说一句,当我尝试将文件下载为 byte[](无流)时,上面的代码有效。
我在网上能找到的就是这个code,它用的是angularJs,而我用的是angular 5,谁能指出问题所在!谢谢。
更新:
我可以在 Google chrome 的网络选项卡中看到文件正在下载,但我不知道文件保存在哪里。
我尝试使用您的后端代码,但在 angular 我使用了这个:
window.location.href = "http://localhost:9001/rest/api/analyses/download";
开始下载成功
我好像漏掉了 around with headers,保存时,这是最终版本,可能对其他人有帮助:
Spring 开机
将这些配置添加到 ApplicationInit:
@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
configurer.setTaskExecutor(asyncTaskExecutor());
}
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new SimpleAsyncTaskExecutor("async");
}
}
这给你的控制器:
@RequestMapping(value = "{analyseId}/download", method = RequestMethod.GET, produces = "application/zip")
public ResponseEntity<StreamingResponseBody> download(@PathVariable Long analyseId) throws IOException {
try {
Analyse analyse = analyseService.getAnalyse(analyseId);
final InputStream file =azureDataLakeStoreService.readFile(analyse.getZippedFilePath());
Long fileLength = azureDataLakeStoreService.getContentSummary(analyse.getZippedFilePath()).length;
StreamingResponseBody stream = outputStream ->
readAndWrite(file , outputStream);
String zipFileName = FilenameUtils.getName(analyse.getZippedFilePath());
return ResponseEntity.ok()
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName)
.contentLength(fileLength)
.contentType(MediaType.parseMediaType("application/zip"))
.body(stream);
} catch (Exception e) {
e.printStackTrace();
return ExceptionMapper.toResponse(e);
}
}
private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
os.write(data, 0, read);
}
os.flush();
}
Angular
download(id) {
let url = URL + '/analyses/' + id + '/download';
const headers = new HttpHeaders().set('Accept', 'application/zip');
const req = new HttpRequest('GET', url, {
headers: headers,
responseType: 'blob',
observe: 'response',
reportProgress: true,
});
const dialogRef = this.dialog.open(DownloadInProgressDialogComponent);
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.DownloadProgress) {
dialogRef.componentInstance.progress = Math.round(100 * event.loaded / event.total) // download percentage
} else if (event instanceof HttpResponse) {
dialogRef.componentInstance.progress = 100;
this.saveToFileSystem(event, 'application/zip');
dialogRef.close();
}
});
}
private saveToFileSystem(response, type) {
const contentDispositionHeader: string = response.headers.get('Content-Disposition');
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], {
type: type
});
saveAs(blob, filename);
}
我使用angular下载大文件,后台我使用spring启动,这里是终点代码:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public StreamingResponseBody download(@PathVariable String path) throws IOException {
final InputStream file =azureDataLakeStoreService.readFile(path);
return (os) -> {
readAndWrite(file , os);
};
}
private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
System.out.println("appending to file");
os.write(data, 0, read);
}
os.flush();
}
当我尝试使用 curl 获取文件时它起作用了,我可以看到正在下载的文件并且它的大小在增加:
curl -H "Authorization: Bearer <MyToken>" http://localhost:9001/rest/api/analyses/download --output test.zip
但是,当我尝试使用 angular 下载文件时它不起作用,即使请求成功,我可以在日志中看到文本 "appending to file" 多次显示,但浏览器上没有下载任何内容,这是我的代码:
this.http.get(url, { headers: headers, responseType: 'blob', observe: 'response' })
.subscribe(response => {
const contentDispositionHeader: string = response.headers.get('Content-Disposition');
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], {
type: 'application/zip'
});
saveAs(blob, filename);
});
saveAs() 属于 file-saver,顺便说一句,当我尝试将文件下载为 byte[](无流)时,上面的代码有效。
我在网上能找到的就是这个code,它用的是angularJs,而我用的是angular 5,谁能指出问题所在!谢谢。
更新:
我可以在 Google chrome 的网络选项卡中看到文件正在下载,但我不知道文件保存在哪里。
我尝试使用您的后端代码,但在 angular 我使用了这个:
window.location.href = "http://localhost:9001/rest/api/analyses/download";
开始下载成功
我好像漏掉了 around with headers,保存时,这是最终版本,可能对其他人有帮助:
Spring 开机
将这些配置添加到 ApplicationInit:
@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
configurer.setTaskExecutor(asyncTaskExecutor());
}
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new SimpleAsyncTaskExecutor("async");
}
}
这给你的控制器:
@RequestMapping(value = "{analyseId}/download", method = RequestMethod.GET, produces = "application/zip")
public ResponseEntity<StreamingResponseBody> download(@PathVariable Long analyseId) throws IOException {
try {
Analyse analyse = analyseService.getAnalyse(analyseId);
final InputStream file =azureDataLakeStoreService.readFile(analyse.getZippedFilePath());
Long fileLength = azureDataLakeStoreService.getContentSummary(analyse.getZippedFilePath()).length;
StreamingResponseBody stream = outputStream ->
readAndWrite(file , outputStream);
String zipFileName = FilenameUtils.getName(analyse.getZippedFilePath());
return ResponseEntity.ok()
.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName)
.contentLength(fileLength)
.contentType(MediaType.parseMediaType("application/zip"))
.body(stream);
} catch (Exception e) {
e.printStackTrace();
return ExceptionMapper.toResponse(e);
}
}
private void readAndWrite(final InputStream is, OutputStream os)
throws IOException {
byte[] data = new byte[2048];
int read = 0;
while ((read = is.read(data)) >= 0) {
os.write(data, 0, read);
}
os.flush();
}
Angular
download(id) {
let url = URL + '/analyses/' + id + '/download';
const headers = new HttpHeaders().set('Accept', 'application/zip');
const req = new HttpRequest('GET', url, {
headers: headers,
responseType: 'blob',
observe: 'response',
reportProgress: true,
});
const dialogRef = this.dialog.open(DownloadInProgressDialogComponent);
this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.DownloadProgress) {
dialogRef.componentInstance.progress = Math.round(100 * event.loaded / event.total) // download percentage
} else if (event instanceof HttpResponse) {
dialogRef.componentInstance.progress = 100;
this.saveToFileSystem(event, 'application/zip');
dialogRef.close();
}
});
}
private saveToFileSystem(response, type) {
const contentDispositionHeader: string = response.headers.get('Content-Disposition');
const parts: string[] = contentDispositionHeader.split(';');
const filename = parts[1].split('=')[1];
const blob = new Blob([response.body], {
type: type
});
saveAs(blob, filename);
}