html代码:
<input type="file" id="selectedFile" (change)="upload($event)" multiple />
角度ts文件
upload(event) {
console.log('input files list : ', event.target.files); // files list showing crrectly
this._createZipFile(event.target.files)
}
private _createZipFile(inputFiles) {
const _zip = new JSZip();
let count = 0;
const zipFilename = 'zipFilename.zip';
// inputFiles.prototype.forEach(function (file) {
const filename = 'filename';
// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(inputFiles[0].file, function (err, data) {
if (err) {
throw err; // or handle the error
}
_zip.file(filename, data, { binary: true });
count++;
if (count === inputFiles.length) {
/* const zipFile = JSZip.generateAsync({ type: 'blob' });
// saveAs(zipFile, zipFilename);
console.log('what is zip file??? ', zipFile); */
if (count === inputFiles.length) {
_zip.generateAsync({type: 'blob'}).then(function(content) {
// saveAs(content, zipFilename);
// i need zip file to send to server
const file: File = newZipFile; // Here need created zip file
const formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
});
}
}
});
// });
}