0

I want to validate existing images size which have been saved before. Value stored in database is just image name.

Here is my html code

<div ng-repeat="image in stored.images">
     <img ng-src="image_folder/{{image.name}}" />
</div>

Is there any way to get image size (in KB/MB not height and width) from image path in angularjs? or should do something in java controller?

rnevius
  • 26,578
  • 10
  • 58
  • 86
greenthunder
  • 697
  • 8
  • 19
  • 34
  • Possible duplicate of [Determining image file size + dimensions via Javascript?](http://stackoverflow.com/questions/1310378/determining-image-file-size-dimensions-via-javascript) – rnevius Oct 27 '15 at 02:40
  • @mevius ain't work for angular, only javascript – greenthunder Oct 27 '15 at 02:57
  • That's wrong. Angular *is* JavaScript (as is the answer you selected as correct) – rnevius Oct 27 '15 at 03:00
  • @mevius i mean there's no default angular function, but yes with javascript – greenthunder Oct 27 '15 at 03:14
  • @mevius He was hoping to find an angular solution, and he chose the javascript solution after realising that there is no way to accomplish this with Angular JS – PC3TJ Oct 27 '15 at 03:34

1 Answers1

2

No this will not be possible with Angular as the only was with javascript to do this is to execute an AJAX request for the image then read the

getResponseHeader('Content-Length')

Header property. Angular JS is not intended for these kinds of operations.

function httpGetFileSize(theUrl){
 //FETCH Data From Server
 xmlhttp=new XMLHttpRequest();
 xmlhttp.open("HEAD", theUrl , false );
 xmlhttp.send();   
 return xmlhttp.getResponseHeader('Content-Length');
}

That is the javascript answer to this problem. You could also do this server side with PHP or ASP

PC3TJ
  • 852
  • 5
  • 16
  • Wouldn't it be more efficient to do a `HEAD` request, rather than a `GET`? Or is there no difference here? – rnevius Oct 27 '15 at 02:52
  • 1
    Yes it would. Sorry thought i changed that when I pasted my normal GET request function in. – PC3TJ Oct 27 '15 at 02:55