【问题标题】:Angular 2- Generate CSV file and download it as Zip FileAngular 2- 生成 CSV 文件并将其下载为 Zip 文件
【发布时间】:2018-07-26 11:04:06
【问题描述】:

目前我正在使用 Angular5Csv 并且已经可以正常下载 CSV 文件。

但现在我的客户想将其下载为 Zip 文件类型。我确实尝试了一些方法,可以将其下载为 Zip 文件类型,但无法解压缩。

下面是我的代码:

downloadLog(hall_id: String, from_date: Date, to_date: Date) {
let params = new HttpParams();

params = hall_id ? params.append('hall_id', hall_id.toString()) : params;
params = from_date ? params.append('from_date', from_date.toString()) : params;
params = to_date ? params.append('to_date', to_date.toString()) : params;
return this.http
  .get(`${this.apiLog}`, {params: params})
  .pipe(
    tap((response: any) => {
        var options = { 
          fieldSeparator: ',',
          quoteStrings: '"',
          decimalseparator: '.',
          showLabels: true, 
          useBom: true,
          noDownload: true,
          headers: ['id', 'delete_flag', 'hall_id', 'send_type', 'type', 'reply_token'
         , 'source_type', 'source_id', 'message_id', 'message_type', 'text', 'title', 'address', 'latitude'
         , 'longitude', 'package_id', 'sticker_id', 'data', 'timestamp', 'created_date', 'updated_date'
         , 'deleted_at']
        };
        this._csv = new Angular5Csv(response.data, 'Log Messages', options);

        const blob = new Blob([this._csv['csv']], {
          type: 'application/zip'
        });
        const url = window.URL.createObjectURL(blob);
        window.open(url);
        // this.log('Download successfully。', 'success', true);
    }),
    catchError(this.handleError('downloadLog', {success: false}))
  )
  .subscribe(res => {
    }, error => {

    }, () => {

    });

}

我是 Angular 的新手,对它了解不多。如果我能得到一些帮助,那将对我有很大帮助。谢谢

【问题讨论】:

标签: angular


【解决方案1】:

它不起作用,因为您只是将 CSV 文件包装成一个 blob,但您从未压缩 CSV。

你可以试试JSZip,还有一些帮助here如何在Angular中使用它。

【讨论】:

  • 嘿@slashpm ...你能分享一些代码吗?在上述情况下我们如何使用 JSzip
猜你喜欢
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多