Determining image file size + dimensions via Javascript?

后端 未结 11 1252
执念已碎
执念已碎 2020-11-22 07:00

As part of a web app, once images have been downloaded and rendered on a web page, I need to determine an image\'s file size (kb) and resolution within the browser context (

11条回答
  •  暖寄归人
    2020-11-22 07:45

    How about this:

    var imageUrl = 'https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg';
    var blob = null;
    var xhr = new XMLHttpRequest(); 
    xhr.open('GET', imageUrl, true); 
    xhr.responseType = 'blob';
    xhr.onload = function() 
    {
        blob = xhr.response;
        console.log(blob, blob.size);
    }
    xhr.send();
    

    http://qnimate.com/javascript-create-file-object-from-url/

    due to Same Origin Policy, only work under same origin

提交回复
热议问题