Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fetch part of a remote image in javascript?

Tags:

javascript

I want to extract EXIF data from remote images using Javascript. It works OK at the moment but it gets quite slow when the image is large since I download the entire image before extracting the EXIF data.

EXIF data is always within the first 128kb of an image (I believe) so I really don't need the entire image.

Is it possible to somehow only fetch the first XXX kb of a remote file with JS?

like image 960
Linus Avatar asked Oct 17 '13 17:10

Linus


1 Answers

A range request works just fine:

$.ajax({ // assuming that you use jQuery
    url: 'http://example.com/images/001.jpg',
    headers: {
        range: 'bytes=0-131071' // inclusive
    },
    complete: function (xhr) {
        var data = xhr.responseText;
        console.log(data.length); // 131072
        console.log(xhr.status); // 206
        yourExifParser(data);
    }
});

Online demo: http://jsfiddle.net/9CknY/1/

But same-origin-policy applies.

like image 166
kay Avatar answered Oct 16 '22 04:10

kay