我有一个网络共享文件夹(\server\ folder ),需要使用angular下载文件(*.zip扩展名)。我找到了一些资源,可以通过rest api从下载文件,但它们没有帮助。要求是(直接从网络共享文件夹)下载它们。
发布于 2019-10-21 06:50:22
在您的服务中,您可以这样做以避免JSON响应
public getFile(): Observable<Blob> {
//const options = { responseType: 'blob' }; there is no use of this
let uri = '/your/uri';
return this.http.get(uri, { responseType: 'arraybuffer' });
}在你的组件中,你可以下载你的文件('ZIP'),如下所示:
public downloadZIP(): void {
this.yourService.downloadFile(filename).subscribe(data => {
const blob = new Blob([data], {
type: 'application/zip'
});
const url = window.URL.createObjectURL(blob);
window.open(url);
});
}在你的RESTController中,你可以拥有类似这样的东西:
@RequestMapping(path = "/downloadZipFile", method = RequestMethod.GET)
public void downloadZIPfile(@RequestParam(value = "zipFileName") String zipFileName, HttpServletResponse response) {
String sharedFolderUri ="path_to_the_shared_folder/"+zipFileName+".zip";
File zipFile = new File(sharedFolderUri );
Path path = Paths.get(zipFile.getAbsolutePath());
try {
System.out.println("File Name :" + zipFile.getName());
InputStreamResource resource = new InputStreamResource(new FileInputStream(zipFile));
return ResponseEntity.ok()
.headers(headers)
.contentLength(zipFile.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}https://stackoverflow.com/questions/58477861
复制相似问题