1

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

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.

Below is my code:

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 => {

    }, () => {

    });

}

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

阮忠仁
  • 21
  • 3
  • are you able to achieve this?..even i have used JSzip which is mentioned by @slashpm but no luck – ResolveError Jun 18 '20 at 12:27
  • 1
    Duplicate question i have found. I have updated answer here -https://stackoverflow.com/questions/62451617/can-we-use-jszip-with-angular7csv-for-zipping-the-multiple-csv-file/62660460#62660460 – TechHelp Jul 03 '20 at 11:44

1 Answers1

0

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

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

slashpm
  • 214
  • 2
  • 9