you can use jszip link https://github.com/Stuk/jszip like

import zipTargetFiles from '/path'

zipTargetFiles( data ).then(file => {
 //operations
})

if you use fetch like this.

fetch('URL', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    //Body
  })
}).then((response)=>{
//here is youu want zip data 
var zip = new JSZip();
var zipData = response.data // 
// Add an top-level, arbitrary text file with contents
zip.file("response.txt", zipData);

// Generate the zip file asynchronously
zip.generateAsync({type:"blob"})
.then(function(content) {
    // Force down of the Zip file
    saveAs(content, "zipFile.zip");
});

}).catch((error)=>{
console.log(error)
})
Answer from errorau on Stack Overflow
🌐
Reddit
reddit.com › r/reactjs › downloading a zip file from api response body in bytes string?
r/reactjs on Reddit: Downloading a Zip File from API Response Body in Bytes String?
April 21, 2019 - https://stackoverflow.com/questions/57593882/react-downloading-a-zip-file-from-api-response-body-in-bytes-string
Discussions

React - Downloading a Zip File from API Response Body in Bytes String?
I have a working API call that returns in the response body a string prepared in Bytes format on the Python side that is a zip file. The String looks something like this, but longer: PK��Q��F������... More on stackoverflow.com
🌐 stackoverflow.com
React and Node app download zip file from REST API
I have a React and Node app and I have a table with download button. When I click the download button it makes a request to the node app which I want it to make a call to a REST API to download a zip More on stackoverflow.com
🌐 stackoverflow.com
November 8, 2021
How to download a .zip file that I recieve from an HTTP response (Axios PUT request)
So the API's response contains a data property which should contain the .zip file that I need. Its written in a format I do not understand. The format: I tried using .blob() as referenced in similar More on stackoverflow.com
🌐 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
🌐 stackoverflow.com
People also ask

What is the best way to handle file downloads in React?
The most efficient way to handle file downloads in React is using an API. This allows files to be downloaded directly on the client side.
🌐
blog.filestack.com
blog.filestack.com › home › react download file from api: a guide
React download file from API: A Guide
What security considerations should you consider when downloading files from an external API?
Regarding security, you should validate file size, format, and type. Moreover, you must implement authorization and authentication techniques.
🌐
blog.filestack.com
blog.filestack.com › home › react download file from api: a guide
React download file from API: A Guide
🌐
GitHub
github.com › Ishaan28malik › react-zip-download
GitHub - Ishaan28malik/react-zip-download: A project to implement multi file zip downloader using Reactjs · GitHub
A project to implement multi file zip downloader using Reactjs - Ishaan28malik/react-zip-download
Starred by 6 users
Forked by 5 users
Languages   HTML 64.0% | CSS 26.7% | JavaScript 9.3%
🌐
StackBlitz
stackblitz.com › edit › zip-file-downloader-react
Zip File Downloader React - StackBlitz
Starter project for React apps that exports to the create-react-app CLI.
🌐
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 - // /pages/api/files.js export default function handler(req, res) { res.status(200).json([ { name: "laptop", url: "https://res.cloudinary.com/dbxcernxw/image/upload/v1674379837/Mridul.Tech Blog/pexels-laptop_tfnlrg.jpg", type: "jpeg", }, { name: "coding-react-js", url: "https://res.cloudinary.com/dbxcernxw/image/upload/v1674379837/Mridul.Tech Blog/pexels-react-code_gkeio5.jpg", type: "jpeg", }, { name: "coding-python", url: "https://res.cloudinary.com/dbxcernxw/image/upload/v1674379837/Mridul.Tech Blog/pexels-python-code_qau1ba.jpg", type: "jpeg", }, { name: "coding-pdf", url: "https://zip-down
Find elsewhere
🌐
Filestack
blog.filestack.com › home › react download file from api: a guide
React download file from API: A Guide
July 19, 2023 - One efficient way is to enable React file downloads using an API. With this method, we get the file data using the API, then interpret it and download it directly on the client side.
🌐
Medium
yashodgayashan.medium.com › zip-files-in-react-30fb77fd6a58
Zip files in React. In this tutorial we will go through how… | by Yashod Perera | Medium
November 3, 2020 - const onChangeFile = () => { const ... name. zip.file(files[file].name, files[file]); } zip.generateAsync({type: "blob"}).then(content => { saveAs(content, "example.zip"); }); } saveAs is taken from the file-saver package in npm ...
🌐
YouTube
youtube.com › watch
Ep6 - React Download Zip File: Create File sharing web app with Express & ReactJs from Scratch - YouTube
Part 6: React Download Zip File use Nodejs ArchiverDescription: Create File sharing web app with Node js, React from scratch.Code: https://github.com/tabvn/...
Published   October 29, 2017
🌐
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) { /
🌐
Stack Overflow
stackoverflow.com › questions › 69883256 › react-and-node-app-download-zip-file-from-rest-api
React and Node app download zip file from REST API
November 8, 2021 - You can do one thing make api request to your source zip file containing rest api from nodejs and get stream data and then pipe it res. In this way you can get stream data directly to react js application from nodejs application
🌐
Filestack
blog.filestack.com › home › generate a zip file with javascript & react – filestack
Generate a Zip File With Javascript & React - Filestack
April 12, 2019 - The pictures uploaded in the sample app can be downloaded as well. To do so we used the zip process API of Filestack. ... Implementing this in React is very simple to achieve. Once you have cloned the repository open: /src/components/Thumbnail.jsx: ...
🌐
YouTube
youtube.com › watch
Easily Zip Files in React - YouTube
Learn how to generate ZIP archives to download multiple files on the fly with JavaScript in React using JSZip and Next.js API Routes.We'll walk through creat...
Published   September 21, 2023
Top answer
1 of 4
3

What I wanted:

  • send files of any formats from the back-end to the front-end

My tools:

  • axios, express, saveAs

The problem I faced with:

  • Unable to download zip file using axios
  • https://github.com/eligrey/FileSaver.js/issues/156
  • https://github.com/axios/axios/issues/448

Nothing helped me, probably because I did something wrong. But here is a simple and quick solution that I came up with:

//BE
const filename = "my-file-name.json";

const zip = new AdmZip();
zip.addFile(filename, body);
const content = zip.toBuffer();

res.set({
            "Content-Length": Buffer.byteLength(content), //I'm not sure if this is necessary, but it's better to let it be :-)
            "Content-Type": "text/plain",
            "Content-Disposition": `attachment; filename=${filename}.${format}`,
          });
res.status(200).send(content.toString("hex")); //my solution to the problem

//FE
const { headers, data } = await axios.post(myEndpoint);
const headerLine = headers["content-disposition"];
const filename = headerLine.replace(/[\w; ]+filename=/g, "");

const content = Buffer.from(data, "hex");
const blob = new Blob([content], { type: "application/zip" });
saveAs(blob, filename); //file-saver npm package

2 of 4
1

Your problem is that you didn't explicitly specify the response type in your PUT request. This should work:

const exportCards = () => {
  axios
    .put(url, {
      ids: ids,
    }, {
      responseType: 'blob'
    })
    .then((res) => { // Now 'res.data' is Blob, not a string
      var file = window.URL.createObjectURL(res.data);
      window.location.assign(file);
    })
    .catch((e) => console.log(e));
};

🌐
Medium
medium.com › yellowcode › download-api-files-with-react-fetch-393e4dae0d9e
Download API Files With React & Fetch | by Manny | yellowcode | Medium
April 11, 2020 - The issue with this is that it leaves the location of the document exposed for further download after the initial request, which might not be that much of a problem. The biggest issue with this is that now, on the server side, we have a physical file to manage. It takes up space and will require clean up. This method is the one we’ll be looking at today, where the file data is sent to us via the API, we interpret that data, and download it directly on the client side, without opening up a new tab.
🌐
Stack Overflow
stackoverflow.com › questions › 73931395 › how-can-i-download-a-zipped-folder-from-a-post-api-in-react
How can I download a zipped folder from a post API in REACT
November 14, 2024 - import { saveAs } from "file-saver"; import JSZip from "jszip"; function Download({ apiToken }) { const [category, setCategory] = useState([]); //click to download function const handleClick = () => { setButtonText("Downloading..."); const params = JSON.stringify({ subcategoryiid: category, }); Axios({ url: `${baseUrl}/cards/details`, method: "POST", responseType: "blob", headers: { Authorization: `Bearer ${apiToken}`, Accept: "application/json", "Content-Type": "application/json", }, body: params, }) .then((res) => { console.log(res); let zip = new JSZip(); let zipData = res.data; zip.folder(