0

I have local image URL and I want to get the blob from it.

The only way I found was to do HTTP request 'get' on the local URL, and read the returned blob... but this is such a strange way.

The code snippet using HTTP:

 function readBody(xhr) {
    var data;
    if (!xhr.responseType || xhr.responseType === "text") {
        data = xhr.responseText;
    } else if (xhr.responseType === "document") {
        data = xhr.responseXML;
    } else {
        data = xhr.response;
    }
    return data;
}

var xhr=new XMLHttpRequest();
xhr.open('GET',results[i],true);
xhr.responseType='blob';
xhr.send();
xhr.onreadystatechange=function()
{
    var blob;
    if(xhr.readyState==4)
    {
        blob=readBody(xhr);
        uploadPhoto(blob,storageRef);

    }
};
5
  • Please, provide some codes. You have tried. Commented Dec 9, 2016 at 13:47
  • @nmnsud I edited the post as required Commented Dec 9, 2016 at 14:00
  • Why does making an HTTP request seem strange? Commented Dec 9, 2016 at 14:01
  • Because the file is local... What is the meaning of get request on local URL? Commented Dec 9, 2016 at 14:02
  • @Dandan It's a web browser. It doesn't to local file system at all. Commented Dec 9, 2016 at 14:04

1 Answer 1

1

Your image needs to be converted to base64 and then from base64 in to binary. This is done using .toDataURL() and dataURItoBlob()

It's pretty fiddly process, I've created a tutorial you can follow which walks you through the process.

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

1 Comment

Thank you for helping! The link is broken, can you fix it please?

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.