Reputation: 135
I would like to know the best way in Javascript (maybe in JQuery) to do the following things without server coding if able :
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)
I would like to ZIP player1.txt until player20.txt into a ZIP file players.zip.
I would like to download the ZIP file players.zip.
Thank you for your help and your answers.
Upvotes: 2
Views: 5834
Reputation: 5848
Sample code with jszip
and FileSaver.js
import JSZip from "jszip";
import { saveAs } from "file-saver";
let zip = new JSZip();
// API call to get articles
downloadArticles(articleIds).then(function (response) {
let records = response.articles;
for (let index = 0; index < records.length; index++) {
let record = records[index];
let blob = getTextBlob(record.title, record.body);
let file_key = index + 1;
let filename = `${file_key}-${record.title}.txt`;
zip.file(filename, blob);
}
zip
.generateAsync({ type: "blob" })
.then(function (content) {
saveAs(content, "articles.zip");
});
});
Upvotes: 1
Reputation: 2337
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.
Upvotes: 4