1

May I ask if any one have any idea how to include a csv file into JSZip? I only know it can have a text file and image folder. How do i include a csv file in it?

My csv download function :

<a href="" ng-click="download()">Download</a>

        //Javascript
        function jsonToCsv(headingRows, dataRows) {
        var csv = headingRows.join(',') + '\n';
        csv += dataRows.map(function (row, i) {
        return Object.keys(row).sort().map(function (columnKey) {
        return row[columnKey];
        }).join(',');
        }).join('\n');
        return csv;
        };
        $scope.download = function () {
        var csv = jsonToCsv(['ThemeName', 'DataSharing', 'DisplaySetting', 
        'ExpiryDate', 'Icon', 'PusblishDate','QueryName','ThemeName', 
        'ThemeOwner'], 
        community)
        var csvBlob = new Blob([csv], { type: 'text/csv' });
        saveAs(csvBlob, 'data.csv');
        };
        $scope.download1 = function () {
        var csv = jsonToCsv(['ThemeName', 'DataSharing', 'DisplaySetting', 
        'ExpiryDate', 'Icon', 'PusblishDate','QueryName','ThemeName', 
        'ThemeOwner'], sports)
        var csvBlob = new Blob([csv], { type: 'text/csv' });
        saveAs(csvBlob, 'data.csv');
        };

And the JSZip function:

             $scope.create_zip = function () {
          var zip = new JSZip();
          zip.file("Hello.csv", "Hello World\n");

          zip.generateAsync({type:"blob"})
          .then(function(content) {
              // see FileSaver.js
              saveAs(content, "example.zip");
          });
        }

So, what I am trying to ask now is, is there like a way for me to include the current csv download function I have into the JSZip folder? Or any other way for me to include csv file into JSZip.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
HangulSR
  • 259
  • 2
  • 10
  • https://stackoverflow.com/questions/51537323/angular-2-generate-csv-file-and-download-it-as-zip-file and https://stackoverflow.com/questions/62451617/can-we-use-jszip-with-angular7csv-for-zipping-the-multiple-csv-file i saw similar question with this but there is no proper answer i ever found – ResolveError Jun 23 '20 at 06:29

1 Answers1

0
    let zip = new JSZip();

for (let i = 0; i < 5; i++) {
    var CSV = 'CSV_content ';

    // Fill CSV variable
    zip.file("file" + i + ".csv", CSV);
}
zip.generateAsync({
    type: "base64"
}).then(function(content) {
    window.location.href = "data:application/zip;base64," + content;
});

here its creating number CSV file inside zip folder like

download.zip -->

  • file 0.csv,
  • file 1.csv,
  • file 2.csv

whatever content is given in CSV will displaying in file.csv make changes according to your need if want to display dynamic CSV file on that you need do these things back-end because of file access not present in the client-side . in back-end create dir store CSV and after the download is done remove dir

PK2995
  • 143
  • 2
  • 9