Angular 2-生成CSV文件并将其下载为Zip文件 [英] Angular 2- Generate CSV file and download it as Zip File

查看:204
本文介绍了Angular 2-生成CSV文件并将其下载为Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Angular5Csv,已经可以正常下载CSV文件了.

Currently I'm using Angular5Csv and can already download CSV file normally.

但是现在我的客户希望将其下载为Zip文件类型.我尝试了一些方法,可以将其下载为Zip文件类型,但是无法解压缩.

But now my client want to download it as Zip file type. I did tried some methods and can download it as Zip file type, but can't unzip it.

下面是我的代码:

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的新手,对此了解不多.如果我能得到一些帮助,那将对我有很大帮助.谢谢

I'm new to Angular and don't know much about it. It would helped me a lot if I could receive some help. Thanks

推荐答案

它不起作用,因为您只是将CSV文件包装到Blob中,而从未压缩CSV.

It is not working because you are just wrapping your CSV file into a blob but you are never zipping the CSV.

您可以尝试 JSZip ,并且有一些帮助

You could try JSZip and there is some help here how to use it with Angular.

这篇关于Angular 2-生成CSV文件并将其下载为Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆