I would like to know the best way in Javascript (maybe in JQuery) to do the following things without server coding if able :
- Generate around 20 text files from sets of objects.
- Zip these files into a ZIP file.
- 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.
I would like to know the best way in Javascript (maybe in JQuery) to do the following things without server coding if able :
- Generate around 20 text files from sets of objects.
- Zip these files into a ZIP file.
- 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.
Share asked Jun 20, 2015 at 9:17 SamuelSamuel 1351 silver badge13 bronze badges2 Answers
Reset to default 4just use some JavaScript zip library eg https://stuk.github.io/jszip/ and a file saver https://github./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.
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");
});
});