如何使用Javascript生成文本文件并下载zip文件? [英] How to generate text files and download a zip file in Javascript?

查看:235
本文介绍了如何使用Javascript生成文本文件并下载zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我想知道Javascript(也许在JQuery中)在不进行服务器编码的情况下执行以下操作的最佳方法:

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


  1. 从一组对象中生成大约20个文本文件。

  2. 将这些文件压缩为ZIP文件。

  3. 下载此ZIP文件。

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






1。从一组对象中生成大约20个文本文件



大约有20组对象,每组对象中大约有90个对象。这是一组对象的示例:


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)

生成的文本文件应为:

player1.txt

3 Noise
1 Parasite
2 Sure Gamble
...

player2.txt

1 Gabriel Santiago
2 Pipeline
2 Aurora
...

...(直到 player20.txt

我想将player1.txt压缩,直到player20.txt压缩为ZIP文件player.zip。

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

我想下载ZIP文件player.zip。

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

谢谢您的支持帮助和您的答案。

Thank you for your help and your answers.

推荐答案

只需使用一些JavaScript zip库,例如 https://stuk.github.io/jszip/ 和文件保护程序 https://github.com/eligrey/FileSaver.js/

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

jszip 提供了将文件放入zipfile的所有必要示例(涵盖第1点和第2点)。对于第3点,使用 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.

这篇关于如何使用Javascript生成文本文件并下载zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆