0

I have a react app in which I have a button. Basically a div.

This is returned from my Button Component and everything else are props.

<button className={"button "+styleButton} onClick={handleClick}>
    {source && <img src={source} alt={alt} className={"image "+styleImage} /> }
    <h3 className="text">{children}</h3>
</button>

Now what I did when someone clicks this Button(div) is :

const handleClick = () => {
    console.log('downloading...');
    let a = document.createElement('a');
    a.href = '192.168.43.102/download/brief';
    a.download = 'brief.pdf';
    a.click();
}

On clicking on that div, I want a pdf to be downloaded on the client side. There are hundreds to ways on the internet. Some send a request with(axios) and download on front-end and then create a downloadable link and some just use an anchor tag.

I tried some of these but I can't make it working. Also on the express side.

This route is my express side:

const router = require('express').Router();
const { Router } = require('express');
const path = require('path');
const { route } = require('./api');

const brief = path.join(__dirname, '..', '/public/resources/krishi-brief.pdf');
const uml = path.join(__dirname, '..', '/public/resources/krishi-uml.pdf');

router.get('/brief', (req, res) => {
    res.sendFile(brief);
});

router.get('/uml', (req, res) => {
    res.download(uml);
});

router.get('/proposal', (req, res) => {
    res.send("Not found");
});

router.get('/ppt', (req, res) => {
    res.send("Not found");
});

module.exports = router;

I have a very good pfd which is non-corrupted but when I get the file, it is corrupted because none of the applications can open it.

I also tried:

router.get('/uml', (req, res) => {
    res.set({
      'Content-Type': 'application/pdf'
    })
    res.download(uml);
});

But now I am confused about the implementation and also if this is the right way.

Please tell me if this is the right(professional) way to do this in react app and where is it wrong? I am only getting corrupted file :(

Shobhit Tewari
  • 535
  • 5
  • 20
  • *Updated Link* See [How can I create download link in HTML?](https://stackoverflow.com/questions/2793751/how-can-i-create-download-link-in-html/16316830#16316830) – Shivam Jha Apr 07 '21 at 18:43
  • If you see my handleClick function, that's what I did. The problem is that file is downloaded beautifully but not opening(corrupted) – Shobhit Tewari Apr 07 '21 at 18:52
  • 1
    `a.href = '192.168.43.102/download/brief';` the `href` does not seems valid. It should be pointing to `pdf` file, i.e., ending with `.pdf`. It should be `http://192.168.43.102/download/brief.pdf` – Shivam Jha Apr 07 '21 at 18:58
  • Doesn't work unfortunately. The file is downloaded and corrupted. – Shobhit Tewari Apr 07 '21 at 19:11
  • Then try to open this URL in browser and see what opens – Shivam Jha Apr 08 '21 at 05:38
  • Same result. File cannot be opened. Should I post a SS of the message or something – Shobhit Tewari Apr 08 '21 at 09:18
  • *Then try to open this URL in browser and see what opens* : if the fine do not open in browser, then the file itself is currupted, you can't do anything – Shivam Jha Apr 08 '21 at 10:16
  • File opens in browser. I made sure it is not corrupted from the start. I made the folder public and given the complete path as a get request from the browser(or typed the path into the search bar) and it opened like normal. – Shobhit Tewari Apr 08 '21 at 10:20

1 Answers1

0

OK so when I click on the download, the server 192.168.43.102 should be written as http://192.168.43.102 and the anchor tag won't give or throw and error but just download something which I am not sure about which is not even on your route.

Basic thing I was struggling on.

Shobhit Tewari
  • 535
  • 5
  • 20