Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate zip file and download in Angular 4?

Tags:

angular

I am beginner in Angular 4. I have refer lots of Links for generate Zip file but I can not get proper solution.
Is it possible to generate Zip file in Angular 4?
If possible, then how?

like image 575
Kiran Joshi Avatar asked Jun 29 '18 10:06

Kiran Joshi


People also ask

How do you make a zip file download?

Right-click on the file or folder.Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

Is a Zip file a container?

A ZIP file is a container for other files. ZIP files compress their contents, which reduces downloading time. To download a ZIP file, click on a link to it; this will prompt your browswer to ask you if you would like to open or save the file.


2 Answers

You can use any JS library in Angular. So for example you can use JSzip and then just:

import * as JSZip from 'jszip';

And now you will have access to this library methods. You can read more about using pure JS libs in angular. There is quite a lot of information about it and even clear instructions how to do it and use properly. I think it will be better if you dive in into this subject more instead of getting clear instruction step by step how to do it. You will learn more and if you will have such a problem in future you will solve it easily. I hope this suggestion will help you.

like image 60
Adrian Sawicki Avatar answered Nov 14 '22 22:11

Adrian Sawicki


Install library

$ npm install --save npm install jszip

In a file tsconfig.json

{"compilerOptions": {"paths": {
  "jszip": ["../node_modules/jszip/dist/jszip.min.js"]
}}}

In a file demo.component.ts

import { saveAs, encodeBase64 } from '@progress/kendo-file-saver';
import * as JSZip from 'jszip';

Function on demo.component.ts

downloadFileExample() {
  const jszip = new JSZip();
  jszip.file('Hello.txt', 'Hello World\n');

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

This example is with Angular 6.

like image 28
sercheo_87 Avatar answered Nov 14 '22 20:11

sercheo_87