首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用angular从网络共享文件夹下载文件

使用angular从网络共享文件夹下载文件
EN

Stack Overflow用户
提问于 2019-10-21 06:31:02
回答 1查看 1.4K关注 0票数 0

我有一个网络共享文件夹(\server\ folder ),需要使用angular下载文件(*.zip扩展名)。我找到了一些资源,可以通过rest api从下载文件,但它们没有帮助。要求是(直接从网络共享文件夹)下载它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-21 06:50:22

在您的服务中,您可以这样做以避免JSON响应

代码语言:javascript
复制
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'),如下所示:

代码语言:javascript
复制
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中,你可以拥有类似这样的东西:

代码语言:javascript
复制
@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();
            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58477861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档