我有一个网络共享文件夹(\ server \ folder),我需要使用angular下载文件(* .zip扩展名)。我发现一些资源可以通过使用rest api来帮助下载文件,但它们没有帮助。要求是直接从网络共享文件夹下载它们。
答案 0 :(得分:1)
在您的服务中,您可以执行类似的操作以避免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();
}
}