JavaScript: Receive as File Object via URL

Asked 2 years ago, Updated 2 years ago, 45 views

Hi, how are you?

I'm writing down the questions because it's ambiguous to ask.

Status is

PDF files are available through the URL.

Take this URL path and

Can I put it right away as a File object?

Well, if you think of it as a code,

let foo = new File( url ); 
// // sample url : http://www.africau.edu/images/default/sample.pdf

I want to receive it like this

I don't know how to access it even if I try to search

It's frustrating ;;

javascript jquery

2022-09-22 18:11

1 Answers

I think you want to do something by getting a pdf from your browser...

async function downloadPDF() {
    // Must be held in the form of a blob.
    const res = await fetch('pdf_link.pdf');
    const blob = await res.blob();

    // How to download via anchor tag
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = 'something.pdf';
    link.innerHTML = 'download';
    document.body.appendChild(link);
}

downloadPDF();

It works with the above code I've tried.

However, only pdf in situations where CORS is allowed can be blob.

Most pdf links on the web where we can find a search are probably impossible because this is not allowed.

Eventually, I have to proxy the link from the server I created and download a pdf that added a CORS permit header.

If this is the case, it would be better to program and receive and process the server from that server.


2022-09-22 18:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.