Skip to content

Instantly share code, notes, and snippets.

@Arunwahatule85
Last active June 27, 2020 08:27
Show Gist options
  • Save Arunwahatule85/3b8ab440c3d72a85e6bd62a4ed225805 to your computer and use it in GitHub Desktop.
Save Arunwahatule85/3b8ab440c3d72a85e6bd62a4ed225805 to your computer and use it in GitHub Desktop.
Download multiple csv files in one zip folder using jsZIP in angular
Below is code in which i am creating data for csv file and downloading csv file in zip folder:
``````````````````
generateCSVfiles()
{
this.service.getStudentDetails(studentid, student.name).subscribe
(res => {
var dataSet = (res);
// start
if (studentid == "Senior") {
const header = ["studentId","StudentName"];
const replacer = (key, values) => values === null ? '' : values // specify how you want to handle null values here
this.seniordata = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
this.seniordata.unshift(header.join(','));
this.seniordata = this.seniordata.join('\r\n');
}
else if (studentid == "Junior") {
const header = ["studentId","StudentName"];
const replacer = (key, values) => values === null ? '' : values // specify how you want to handle null values here
this.juniordata = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
this.juniordata.unshift(header.join(','));
this.juniordata = this.juniordata.join('\r\n');
}
this.downloadzip();
}
}
public downloadzip()
{
let zip = new JSZip();
// Fill CSV variable
zip.file( 'Studentrecord'+'.csv', this.juniordata);
zip.file( 'Studentrecord'+'.csv', this.seniordata);
zip.generateAsync({
type: "base64"
}).then(function (content) {
window.location.href = "data:application/zip;base64," + content ;
});
}
`````````````````````
Above code working fine and able to download csv file inside zip folder when we got response data for one of junior or Senior student. but when we received both junior and senior data at time then it downloading different zip file or sometimes only one zip file getting generated. but i want both file in one zip folder. can someone guide me how to download different csv file in one zip folder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment