0

I'm trying to get BLOB from local file URL. This is my code.

let url ="file:///storage/emulated/0/Download/AssetInfoXMLOxk!B1568355279906/1568355272623.jpeg";
fetch(url).then((res) => res.blob()).then((blob) => {
  console.log(blob);
})

Unfortunately i'm not able get blob because of this error. from origin 'http://localhost:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, https. How can I get blob from local file URL? Thanks in advance.

8
  • Show fetch(url) Commented Sep 13, 2019 at 6:31
  • @AdritaSharma. file:///storage/emulated/0/Download/AssetInfoXMLOxk!B1568355279906/1568355272623.jpeg Commented Sep 13, 2019 at 6:33
  • Show the function!! Commented Sep 13, 2019 at 6:34
  • @AdritaSharma. I have already added code in snippet. fetch(url).then((res) => res.blob()).then((blob) => { console.log(blob); }) Commented Sep 13, 2019 at 6:36
  • Go to defination of fetch Commented Sep 13, 2019 at 6:38

2 Answers 2

1

Try like this:

this.http.get(url, { responseType: 'blob' }).subscribe((resp: any) => {
   ...
});
Sign up to request clarification or add additional context in comments.

Comments

0

this works form me, I am using file saver to save file in angular.

downlode_file.service:->

    downlode_file() {
     const downlode_file_url = "url"
      let headers = new Headers({ 
        'Content-Type':'application/json', 
        'Accept': 'application/.xls' //give file extension
     });
     let options = { headers : headers };
      return this.http.post(downlode_file_url,options,{responseType: "blob"});
    }

components.ts:->

this.downlode_file.downlode_file().subscribe((file: Blob) => {
  FileSaver.saveAs(file,'filename.xls');
  console.log("Downloaded Successfully...")
}, (err) => {
  console.log("Download Error:", err);
});

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.