使用angular下载zip文件

时间:2017-05-22 12:06:46

标签: rest angular typescript

现在已经好几个小时了,因为我试图找出如何使用angular下载zip文件。 下载的文件比原始文件大。我点了这个链接How do I download a file with Angular2

出于身份验证原因,我不是简单地使用<a>标记进行下载。

服务

 downloadfile(filePath: string){
        return this.http
            .get( URL_API_REST + 'downloadMaj?filePath='+ filePath)
            .map(res => new Blob([res], {type: 'application/zip'}))
    }

组件

downloadfileComponent(filePath: string){
        this.appService.downloadfile(filePath)
            .subscribe(data => this.getZipFile(data)),
                error => console.log("Error downloading the file."),
                () => console.log('Completed file download.');
    }


getZipFile(data: any){
        var a: any = document.createElement("a");
        document.body.appendChild(a);

        a.style = "display: none";
        var blob = new Blob([data], { type: 'application/zip' });

        var url= window.URL.createObjectURL(blob);

        a.href = url;
        a.download = "test.zip";
        a.click();
        window.URL.revokeObjectURL(url);

    }

休息api

public void downloadMaj(@RequestParam(value = "filePath") String filePath, HttpServletResponse response) {

        System.out.println("downloadMaj");
        File fichierZip = new File(filePath);

        try {
            System.out.println("nom du fichier:" + fichierZip.getName());
            InputStream inputStream = new FileInputStream(fichierZip);

            response.addHeader("Content-Disposition", "attachment; filename="+fichierZip.getName());
            response.setHeader("Content-Type", "application/octet-stream");

            org.apache.commons.io.IOUtils.copy(inputStream, response.getOutputStream());
            response.getOutputStream().flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

任何人都可以说出为什么没有下载所有文件?

解决

downloadfile(filePath: string) {
        return this.http
          .get( URL_API_REST + 'download?filePath=' + filePath, {responseType: ResponseContentType.ArrayBuffer})
          .map(res =>  res)
      }
private getZipFile(data: any) {
    const blob = new Blob([data['_body']], { type: 'application/zip' });

    const a: any = document.createElement('a');
    document.body.appendChild(a);

    a.style = 'display: none';    
    const url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = test.zip;
    a.click();
    window.URL.revokeObjectURL(url);

  }

3 个答案:

答案 0 :(得分:3)

  1. 在responseType中,您需要指定一个字符串,在本例中为arraybuffer Angular 5 +
  2. downloadFile(filename: string) {
      return this.http.get(URL_API_REST + 'download?filename=' + filename, {
        responseType: 'arraybuffer'
      });
    }

    1. 我们可以使用下一个代码直接下载我们的文件:
    2. this.myService.downloadFile(filename).subscribe(data => {
        const blob = new Blob([data], {
          type: 'application/zip'
        });
        const url = window.URL.createObjectURL(blob);
        window.open(url);
      });

答案 1 :(得分:1)

在Angular中,不需要jsZip-util,您可以使用标头选项简单地进行服务调用。

public zipAndDownload(url): Observable<any> {
       const options:any = {
        headers: new HttpHeaders({'Content-Type': 'file type of an particular document'}),
        withCredentials: true,
        responseType:'arraybuffer'
      }; 
        return this.http.get<Content>(url,options);

      }

答案 2 :(得分:0)

There are multiple plugins you'll need to get zip download working using angular:

  1. angular-file-saver.bundle

    This plugin will bundle Blob.js and FileSaver.js follow all instructions now just add dependencies on your controller and module.

    ZoneId z = ZoneId.of( "Europe/Paris" ) ; ZonedDateTime zdt = instant.atZone( z ) ;

  2. add JSZip and JSZipUtils

Include files:jszip.js, jszip-utils.js, angular-file-saver.bundle.js

Date