问题描述
是用于以zip格式下载csv文件的代码,但是不确定在我们同时使用两个插件时是否支持该格式,因为出现此错误:无法读取'pdfs/[object Object]'的数据.它是否受支持的JavaScript类型(String,Blob,ArrayBuffer等)? 在jszip.js:3472
below is code for downloading csv file in zip format but am not sure whether it support or not when we use both plugin at time because am getting this error: Can't read the data of 'pdfs/[object Object]'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ? at jszip.js:3472
import { AngularCsv } from 'angular7-csv';
var data = [
{
name: "Test 1",
age: 13,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
{
name: 'Test 2',
age: 11,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
];
this.reportCSV =new AngularCsv(data, 'My Report');
downloadZip() {
var zip = new JSZip();
var pdf = zip.folder("Reports");
this.reportCSV.forEach((i) => {
pdf.file(i+'.csv', { base64: true });
});
zip.generateAsync({ type: "blob" }).then(function (content) {
FileSaver.saveAs(content, "example.zip");
});
}
某些东西需要添加代码,或者无法将angular7csv与jsZip结合在一起?
somethings needs to add in code or it is not possible to combine angular7csv with jsZip?
推荐答案
否! Angularcsv是独立软件包,仅用于下载csv格式文件.它会自动以逗号分隔的格式将您的数据与标头一起转换.处理完数据后,新的AngularCsv(数据,我的报告");此行触发下载并下载文件.有关更多信息,请 https://www.npmjs.com/package/angular7-csv
No! Angularcsv is independent package only used to download the csv format file. It automatically convert your data in comma separated format along with your header. After processing your data new AngularCsv(data, 'My Report'); this line trigger the download and file get downloaded. For more info https://www.npmjs.com/package/angular7-csv
现在要问您的问题,您可以通过传统的代码方式在zip文件夹功能中实现csv文件
Now to come to your question you can achieve csv file in zip folder functionality by traditional way of code
const header = ["name","age","average","approved","description"];
const replacer = (key, values) => values === null ? '' : values // specify how you want to handle null values here
this.csvdata = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
this.csvdata.unshift(header.join(','));
this.csvdata = this.csvdata.join('\r\n');
并将this.csvdata传递给downloadzip()方法
and pass the this.csvdata to downloadzip() method
downloadZip()
{
zip.file("Myfile", this.csvdata);
zip.generateAsync({ type: 'blob' }).then((content) => {
if (content) {
FileSaver.saveAs(content, name);
}
});
}
这篇关于我们可以在Angular7Csv中使用JSZip来压缩多个csv文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!