简体   繁体   中英

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 file in the browser.

I managed to download the zip file by linking the REST API url directly from React but my manager told me not to expose the API url and mask it through Node.

I tried different methods that I found on the web but none of them work, just one that downloaded a zip that contained a file that contained base64 of the files probably.

I would like a solution both for React and Node how to download from a REST API.

Thanks

React code:

 $("#example tbody").on('click', '#buton123', function() {
   
   
  var 
   id=response.data.mesaje[$(this).closest('tr').index()].id;
 
   article2 = {id2:id} 
   axios.post('http://localhost:3001/descarcare',article2)
   .then((respo 
    nse)=>{
   //How to download zip from node 
   })

   });

Node code:

app.post('/descarcare', function(req, res) {

var id = req.body.id2;
console.log(id);


axios.get('http://restapi.com/aaa?id=123').then((response)=>{
//Code to download zip from rest api
)}




});

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

检查可用于压缩内容的jszipjszip-utils库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM