jQuery how to check if uploaded file is an image without checking extensions?

前端 未结 7 1282
有刺的猬
有刺的猬 2021-01-31 09:17

Newbie here. The problem is that I currently have written a method which checks uploaded file size and extension in order to validate it. However, checking extensions is not a s

7条回答
  •  清歌不尽
    2021-01-31 09:59

    Pls refer a related query here. The answer here suggests to load the image in an Image object and check for it's width and height properties to be non zero. I think the technique can be used to solve your problem too.

    I also worked out a fiddle for you to refer. Pertinent code below:

    var img = new Image();
    img.addEventListener("load",function(){
      alert('success');
    });
    img.addEventListener("error",function(){
          alert('error');
    });
      img.src = picFile.result; 
    

提交回复
热议问题