0

I have Node.Js backend and react front-end. I have a query in the backend that returning data based on category name that I enter in an input box. Before I add onClick event in the button it returned the results in a CSV and download it for me. After adding onClick to the button I can see that at the backend I have a successful query but at the front-end, it's not downloading my results in a csv file. I'm using the button from Semantic UI React and CSVLINK from 'react-csv' package.

Here is my code(function that I called in onClick):

handleReportButton(e){

e.preventDefault();
const value = e.target.value;

fetch(`/reportOnAnswersCategory`,{
method: 'POST',
body: JSON.stringify({
  answer: value
}),
headers: {"Content-Type": "application/json"}
  })
.then(data => data.json())
.then((data) => {this.setState({report : data});})
.catch(err => console.error(err));
}

This is the button(I have binded the function before, and I did 'const { report } = this.state;' before return):

<Button onClick={this.handleReportButton}>
  <CSVLink data={report} >Download this report</CSVLink>
</Button>

Thanks in advance for the help

4
  • handleReport is asynchronous, so the report data isn't ready until the fetch results come back. Whereas the CSVLink expects the data there already. You can use CSVDownload to let the link do the downloading too. Commented May 13, 2018 at 2:47
  • @Jayce444 Thank you, I'm using this now after you have suggested but how can I prevent CSVDownload from triggering on mounting the component? It triggers downloading ONLY on mounting the component! Commented May 13, 2018 at 3:48
  • Can you show how you've implemented the CSVDownload? Commented May 13, 2018 at 3:50
  • @Jayce444 I have tried 2 ways: 1: I have changed the '<CSVLink ....' line in button with => <CSVDownload data={report} target="_blank" /> , this way it's triggering after call component 2: I have added this line after '.catc(...' in handleReport => return (<CSVDownload data={this.state.report} target="_blank" />); this way nothing happend Commented May 13, 2018 at 4:02

1 Answer 1

0

To download a file in html5 what you can do is, to set an anchor tag with download attribute,

<a href="/reportOnAnswersCategory" download="data.csv" id="btnExport" >Export data into Excel</a>

And when the user clicks the link, the download attribute appears in the save. In this case, the file will be downloaded as “data.csv”

But to pass the 'answer:value' to the api either you can change the node to accept it as a query param and change the url to href="/reportOnAnswersCategory?answer="+{value}, Or you can hide the <a> using css style,display:none and onClick of your button, do document.getElementById("btnExport").click()

Edit: When the download is using a POST request,

fetch(service_url, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },

  }).then(response => response.blob())
    .then(response => {
  var blob=response
  var reader = new window.FileReader();
  reader.readAsDataURL(blob);
  reader.onloadend = function() {
  var base64data = reader.result;

      window.open(base64data);

  }
})
.catch(error => {
  console.error(error);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Rohith but I'm not using jquery
You dont have to implement the jquery function, you can use the <a> and download attribute with href value as the url which can be set ussing your react code
Removed the jQuery code and provided two possible solutions
Did u get the solution?
unfortunately not yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.