简体   繁体   中英

How to call a node.js API from react to download a file

Currently my API is connected to the Dataverse which responds with an Object with a file information, and the information I have in the API to send to the frontend is for example:

{
  id:1;
  filename: example.pdf ;
  documentbody: (really long string);
  mimetype:'application/pdf'
}

And I want a user in React to be able to download this file if a button is clicked. I have searched but haven't yet found a way to trigger the download in the backend neither in the frontend.

in Ur download function create an anchor tag programmatically set href and download stuff and then trigger the click event programmatically to start downloading.

download(){
    const downloadLink = document.createElement('a');

    downloadLink.href = linkSource;
    downloadLink.download = filename;
    downloadLink.click();
}

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