0

Is it possible to read binary file from url?

var url = 'http://example.com/image/abc.jpg';
var reader= new FileReader();

reader.addEventListener('load', function () {
    // whatever
});
reader.readAsArrayBuffer(url);

Above example is not working (url is not a object).

var file = new File([""], "url");

is empty

2 Answers2

1

URL in your case is a string object. You need to use HTTP get in this case. HTTP GET request in JavaScript? and read the response.

Community
  • 1
  • 1
James
  • 1,436
  • 1
  • 13
  • 25
1

You have to use HTTP GET to get the file. Maybe this is what you need?

function httpGetAsync(theUrl, callback) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.response);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

httpGetAsync("http://cdn.androidbeat.com/wp-content/uploads/2015/12/google-logo.jpg", res => console.log(res))