0

I am getting some file path from sever using API, but here I want to download those file if click on them. I am using react js-file download package. what I have code so far is below.

        downloadFileforUser =(path)=>{
         var data = (baseUrl+'/SurveyImages/'+path )
         fileDownload(data, path);

 }

it is downloading file but I can not read those file such pdf, jpg,png, please help me I am new to Reactjs Thanks

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
K.S
  • 443
  • 10
  • 29

2 Answers2

0

This is not related to React. However, you can use the download attribute on the anchor element to tell the browser to download the file.

<a href='your file path' download>Click to download</a>
niks
  • 426
  • 4
  • 9
0

This is not related to reactjs but it's javascript related so you can take a look at this sample

In mozilla docs example:

var myHeaders = new Headers();

var myInit = { method: 'GET',
               headers: myHeaders,
               mode: 'cors',
               cache: 'default' };

var myRequest = new Request('flowers.jpg', myInit);

    fetch(myRequest).then(function(response) {
      return response.blob();
    }).then(function(myBlob) {
      var objectURL = URL.createObjectURL(myBlob);
      myImage.src = objectURL;
    });

You can read full code here

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60