Home > Net >  Is there a way to know does Link to pdf will not work on click?
Is there a way to know does Link to pdf will not work on click?

Time:10-21

I have a.href link that triggers the URL which returns the PDF and displays it in a new tab.

Is there a way to know does that link will generate the pdf or will work?

I want to display an error message to the user if the URL does not work or a pdf is not generated.

Is that possible?

      <a
        href={URL}
        rel="noopener noreferrer"
        target="_blank"
        download
      >
        PDF DOCUMENT
      </a>

CodePudding user response:

You can use fetch to send request and get the response and then check to see it is a valid response or not:

const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id: 'fileId'})
    };
fetch('https://reqres.in/api/posts', requestOptions)
    .then(response => response.json())
    .then(data => {
         //here check to see data is valid or not
   });
  • Related