1

I get from the server an object that should represent a file, and it looks like this:

name: "סריקה0252.pdf",
url: "https:XYZ/ABC/1/סריקה0252_28-05-2019_11:24:40.pdf"

Now, I want to convert it to a file of JavaScript ,

Is it possible to do so?

5
  • do you mean you would like js to parse this? or you want an actual file myfile.js? if you want an actual file you would need a server sided language. Commented May 28, 2019 at 13:00
  • See Blob. Commented May 28, 2019 at 13:02
  • you can convert this file to the blob. from that file url. Commented May 28, 2019 at 13:02
  • @Ashish, Can you demonstrate how? Commented May 28, 2019 at 13:03
  • @BaruchG. posted. Commented May 28, 2019 at 13:09

1 Answer 1

2
function urlToBlob(url){
 return new Promise((resolve,reject)=>{
    var xhr = new XMLHttpRequest();
    xhr.open( "GET", url, true );
    xhr.responseType = "blob";
    xhr.onload = function( e ) {
        resolve(this.response)
    };
    xhr.onerror = function( error ){
        reject(error)
    }
    xhr.send();
 })
}
let fileUrl = "https:XYZ/ABC/1/סריקה0252_28-05-2019_11:24:40.pdf"
urlToBlob(fileUrl).then(function(blob){
  console.log(blob)
  // you will get blob object of that file here
})

Here is the function to convert it. this will load the file first to the local. once it will be loaded, it will return blob object as return type is defined as a blob.

Sign up to request clarification or add additional context in comments.

Comments

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.