Image file size from data URI in JavaScript

后端 未结 4 1225
野趣味
野趣味 2020-12-18 11:19

I am not sure it\'s even possible but - can I get the image file size from data URI?

For example, let\'s say there is an IMG element where src goes:

4条回答
  •  失恋的感觉
    2020-12-18 12:19

    If you want file size, simply decode your base64 string and check the length.

    var src ="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/// yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
    
    var base64str = src.substr(22);
    var decoded = atob(base64str);
    
    console.log("FileSize: " + decoded.length);

提交回复
热议问题