How to generate text files and download a zip file

2019-05-30 17:38发布

I would like to know the best way in Javascript (maybe in JQuery) to do the following things without server coding if able :

  1. Generate around 20 text files from sets of objects.
  2. Zip these files into a ZIP file.
  3. Download this ZIP file.

1. Generate around 20 text files from sets of objects

There are around 20 sets of objects, with around 90 objects in each set of objects. Here is an example of the sets of objects :

var cardsPlayer1 = {
  name : "Player 1",
  [
    {nbCopies : 3, name : "Noise"},
    {nbCopies : 1, name : "Parasite"},
    {nbCopies : 2, name : "Sure Gamble"},
    ... (around 90 of these objects)
  ]
};

var cardsPlayer2 = 
  name : "Player 2",
  [
    {nbCopies : 1, name : "Gabriel Santiago"},
    {nbCopies : 3, name : "Pipeline"},
    {nbCopies : 2, name : "Aurora"},
    ... (around 90 of these objects)
  ]
};

... (until cardsPlayer20)

The generated text files should be :

player1.txt

3 Noise
1 Parasite
2 Sure Gamble
...

player2.txt

1 Gabriel Santiago
2 Pipeline
2 Aurora
...

...(until player20.txt)

2. Zip these files into a ZIP file

I would like to ZIP player1.txt until player20.txt into a ZIP file players.zip.

3. Download this ZIP file

I would like to download the ZIP file players.zip.

Thank you for your help and your answers.

1条回答
闹够了就滚
2楼-- · 2019-05-30 17:44

just use some JavaScript zip library eg https://stuk.github.io/jszip/ and a file saver https://github.com/eligrey/FileSaver.js/.

jszip provides all necessary examples to put files into the zipfile (this covers points 1 and 2). Using FileSaver.js is also pretty straightforward as for point 3.

查看更多
登录 后发表回答