figured I would share my solution, as i've seen alot of questions about this, but not many helpful answers.
using JSZip: https://stuk.github.io/jszip/
using saveAs from fileSaver.js: https://github.com/eligrey/FileSaver.js
First: it loops through downloading each file individually from s3, creates a blob, and puts each blob/file into a "folder".
Second: after each file has been downloaded from s3, it will then use the saveAs method to download/save the zipped folder.
function NewEditInfo(props) {
const [job, setJob] = useState([]);
const [imagesUploaded, setImagesUploaded] = useState(null);
//initialize jsZip
var JSZip = require("jszip");
let zip = new JSZip();
let photoZip = zip.folder(`${job.streetAddress}`);
useEffect(() => {
//// API call to load job info from dynamo db
function loadJob() {
return API.get("api name", "/tablename/tableinfo", {
'queryStringParameters': {jobId: props.match.params.id}
});
}
/// onload function to load job info and get a count of files
async function onLoad() {
try {
const job = await loadJob();
setJob(job[0])
const ImagesUploaded = await Storage.list(`${job[0].jobId}`);
setImagesUploaded(ImagesUploaded);
} catch (e) {
alert(e);
}
}
onLoad();
}, [props.match.params.id,]);
/// On download button click, loop through / download images using the key given from storage.list in onload function
async function handleDownloadClick(event) {
event.preventDefault();
const imagesAsArray = [...imagesUploaded];
for (let i = 0; i < imagesAsArray.length; i++) {
await DownloadFileFromS3(imagesAsArray[i]);
}
/// waits for "await DownloadFileFromS3, then executes the save as which saves the zipped folder created in "DownloadFileFromS3"
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, `${job.streetAddress}`);
});
}
/// download each file from s3 and put it in the zip folder
async function DownloadFileFromS3(fileToDownload) {
const result = await Storage.get(fileToDownload.key, {download: true})
let mimeType = result.ContentType
let fileName = fileToDownload.key
let blob = new Blob([result.Body], {type: mimeType})
photoZip.file(fileName[1], blob)
}
return(
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonBackButton/>
</IonButtons>
<IonTitle>Download Images</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent className="ion-padding">
<IonButton onclick={handleDownloadClick}> download </IonButton>
</IonContent>
</IonPage>
);
}
export default withRouter(NewEditInfo);
Answer from Jordan Spackman on Stack OverflowGitHub
gist.github.com › c4software › 981661f1f826ad34c2a5dc11070add0f
Download multiple files then compress to one zip file using JSZip & JSZip-utils · GitHub
function test() { var urls = [ 'https://www..../Cani-spiaggia.png', 'https://www..../39052fe544a95dbf6ccfdd106fcabb3e_L.jpg' ]; var zip = new JSZip(); var count = 0; var count2 = 0; var zipFilename = "zipFilename.zip"; var nameFromURL = []; var the_arr = ""; for (var i = 0; i< urls.length; i++){ the_arr = urls[i].split('/'); nameFromURL[i] = the_arr.pop(); } urls.forEach(function(url){ var filename = nameFromURL[count2]; count2++; // loading a file and add it in a zip file JSZipUtils.getBinaryContent(url, function (err, data) { if(err) { throw err; // or handle the error } zip.file(filename, d
Download a zip file in reactjs without any plugins - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Stack Overflow chat opening up to all users in January; Stack Exchange chat... 39 How to save binary data of zip file in Javascript? More on stackoverflow.com
reactjs - React-tsx-Download Multiple image file in Zip format - Stack Overflow
I want to download zip which contains the multiple images, I use the js-zip but it does not allow me to use jszip-util its show Can not find name JSZipUtils for that because of my TSX file i think. here is some code which I tried.. import * as React from 'react'; import JSZip from 'jszip'; ... More on stackoverflow.com
reactjs - Download and zip files in React? - Stack Overflow
I have an application that stores consultations for users. The consultation has data about the user and has documents attached to the consultation. My goal is to return all documents that were atta... More on stackoverflow.com
javascript - Download single file as pdf and multiple files as zip using axios in React - Stack Overflow
I am currently using this method to download files coming from the API. But here whether the file is single or multiple files they are being downloaded as Zip. More on stackoverflow.com
Videos
GitHub
github.com › Ishaan28malik › react-zip-download
GitHub - Ishaan28malik/react-zip-download: A project to implement multi file zip downloader using Reactjs
Starred by 6 users
Forked by 5 users
Languages HTML 64.0% | CSS 26.7% | JavaScript 9.3% | HTML 64.0% | CSS 26.7% | JavaScript 9.3%
JSFiddle
jsfiddle.net › Victornpb › b78wbaL8
download multiple files and save as zip bundle - JSFiddle - Code Playground
March 11, 2013 - JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
CodeSandbox
codesandbox.io › s › download-multiple-images-as-a-zip-with-jszip-file-saver-and-reactjs-iykufe
Download multiple images as a zip with jsZip, file-saver and Reactjs - CodeSandbox
September 28, 2023 - Download multiple images as a zip with jsZip, file-saver and Reactjs by ทíทó using file-saver, jszip, jszip-utils, react, react-dom, react-scripts
Mridul
mridul.tech › home › blogs › how to generate zip with file links in next js and react js
How to Generate ZIP with File Links in Next JS and React JS
December 2, 2023 - Also Read: 10 React Project Ideas to Boost Your Portfolio · 5. Now let’s fetch the file URLs and get the files. We will map over the array and fetch each file from the object. And we can also name the files different name as well. // /pages/index.js import JSZip from "jszip"; const zip = new JSZip(); const remoteZips = files.map(async (file) => { const response = await fetch(file.url); const data = await response.blob(); zip.file(`${file.name}.${file.type}`, data); return data; });
DEV Community
dev.to › jaydeepkhachariya › how-make-zip-of-images-and-download-it-using-react-js-in-3-easy-steps-lkb
How make zip of images and download it using React JS in 3 Easy steps - DEV Community
January 27, 2023 - import { useState, useCallback } from "react"; import JSZip from "jszip"; export default Download(){ const [images,setImages]=useState([ // Array of images "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", ]) const zip = new JSZip(); // instance of JSZip // Function for make zip file and download it async function handleZip(){ // Add Images to the zip file for (var i = 0; i < images.length; i++) { const response = await fetch(images[i]); const blob = await response.blob(); console.log(blob); zip.file(images[i].split("/").pop(), blob); if (i == selectedImages.length - 1) { /
Medium
shubhamjain-84217.medium.com › downloading-multiple-files-with-react-42706b0e765c
Downloading multiple files with React | by Shubham Jain | Medium
March 24, 2021 - so we make a Downloader component and will render this component with the files as a prop conditionally on user action. This solves the cons we faced with the first solution but I am not sure if this is the best solution out there considering the reputation of iframes, sharing some links which might help you to decide whether to go for this solution: ... A better solution could be to have a single downloadable URL generated after zipping all your files at the backend and use that instead to avoid loading too many iframes in your current window or avoiding the cons of the first solution.
npm
npmjs.com › package › multiple-downloads-one-zip
multiple-downloads-one-zip - npm
June 10, 2024 - // require the module let download = require('multiple-downloads-one-zip') // Urls where files are downloaded from let urls = [ "http://i996.imggur.net/hunter-x-hunter/390/hunter-x-hunter-11553019.jpg", "http://i996.imggur.net/hunter-x-hunter/390/hunter-x-hunter-11553025.jpg", ] let zip_name = "myZip" // avoid spaces let zip_dir = "." // give an existing directory // download and zip the files // Since it is an async function, we must put `await` before, and the function must be called in an `async` function await download.downloadAndZip(urls,{ zip_name: zip_name, zip_dir: zip_dir, onEnd: (data) => { // do what we want with the data containing the zip path }, onFileComplete: (data) => { // do what we want } })
» npm install multiple-downloads-one-zip
Published Nov 07, 2023
Version 0.1.4
Stack Overflow
stackoverflow.com › questions › 58143424 › react-tsx-download-multiple-image-file-in-zip-format
reactjs - React-tsx-Download Multiple image file in Zip format - Stack Overflow
July 14, 2022 - JSZipUtils.getBinaryContent(url, function (err, data) { if (err) { throw err; } zip.file(filename, data, { binary: true }); it works very well because jszip-utils module is not available in typescript i think I am not sure, so it is the alternate ...
Stack Overflow
stackoverflow.com › questions › 63989778 › download-single-file-as-pdf-and-multiple-files-as-zip-using-axios-in-react
javascript - Download single file as pdf and multiple files as zip using axios in React - Stack Overflow
April 12, 2024 - try { const res = await axios.get( `${apilink}/download/`, { responseType: "blob", params: body, } ); const url = window.URL.createObjectURL(new Blob([res.data])); const link = document.createElement("a"); link.href = url; link.setAttribute("download", "file.zip"); document.body.appendChild(link); link.click(); } catch (e) { toaster.notify("Cannot Download.Please try again"); }