如果可能的话,我想知道 Javascript(也许是 JQuery)中无需服务器编码即可执行以下操作的最佳方法:
- 从一组对象生成大约 20 个文本文件。
- 将这些文件压缩为 ZIP 文件。
- 下载此 ZIP 文件。
1。从一组对象生成大约 20 个文本文件
大约有 20 组对象,每组对象中有大约 90 个对象。以下是对象集的示例:
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)
2。将这些文件压缩为 ZIP 文件
我想将player1.txt直到player20.txt压缩成ZIP文件players.zip。
3。下载此 ZIP 文件
我想下载 ZIP 文件players.zip。
感谢您的帮助和回答。
最佳答案
只需使用一些 JavaScript zip 库,例如 https://stuk.github.io/jszip/和文件保护程序https://github.com/eligrey/FileSaver.js/ .
jszip
提供了将文件放入 zip 文件的所有必要示例(涵盖第 1 点和第 2 点)。对于第 3 点,使用 FileSaver.js
也非常简单。
关于javascript - 如何在 Javascript 中生成文本文件并下载 zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952313/