Home > Web Front-end > JS Tutorial > How to get the uploaded image type and size

How to get the uploaded image type and size

php中世界最好的语言
Release: 2018-06-08 14:28:45
Original
2449 people have browsed it

This time I will show you how to get the type and size of the uploaded image, and what are the precautions for getting the type and size of the uploaded image. The following is a practical case, let's take a look.

JQuery is used here to determine the type and size of the uploaded image:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="">
  <input type="file" id="file" />
</form>
<p id="p_1">图片格式为:</p>
<p id="p_2">图片大小为:</p>
<script src="js/jquery-1.8.3.min.js"></script>
<script>
$(function(){
  var p_1 = $(&#39;#p_1&#39;),
    p_2 = $(&#39;#p_2&#39;);
  $(&#39;body&#39;).on(&#39;change&#39;,&#39;#file&#39;,function(){
    var path = $(this).val(),
    extStart = path.lastIndexOf(&#39;.&#39;),
    ext = path.substring(extStart,path.length).toUpperCase();
    //判断图片格式
    if(ext !== &#39;.PNG&#39; && ext !== &#39;.JPG&#39; && ext !== &#39;.JPEG&#39; && ext !== &#39;.GIF&#39;){
      alert(&#39;请上传正确格式的图片&#39;);
      resetFile();
      return false;
    }else{
      p_1.html(&#39;图片格式为:&#39; + ext);
    }
    //获取图片大小,注意使用this,而不是$(this)
    var size = this.files[0].size / 1024;
    if(size > 10240){
      alert(&#39;图片大小不能超过10M&#39;);
      resetFile();
      return false;
    }else{
      p_2.html(&#39;图片大小为:&#39; + size.toFixed(2) + &#39;KB&#39;);
    }
  })
  //还原
  function resetFile(){
    //清空file表单的值,不能直接使用$(&#39;#file&#39;).val(&#39;&#39;)这种写法
    $(&#39;form&#39;).html(&#39;<input type="file" id="file" />&#39;);
    p_1.html(&#39;图片格式为:&#39;);
    p_2.html(&#39;图片大小为:&#39;);
  }
})
</script>
</body>
</html>
Copy after login

lastIndexOf()The method retrieves the specified string from back to front. If the specified character appears, Then return the position of the character, if not, return -1, the position starts counting from 0

toUpperCase() method converts to uppercase letters

substring () method intercepts a string. The first parameter is the starting position, and the second parameter is the ending position (if omitted, the end of the string will be intercepted by default), which is the same as slice()# The difference between ## and substr() is that substring() does not accept negative parameters

slice()The method is the same as substring() method, the difference is that it accepts negative parameters (if the parameter is a negative number, the position is calculated from the end of the string)

substr() method intercepts the string, The first parameter is the starting position, and the second parameter is the length of interception (different from slice and substring). It is no longer recommended to use

I believe you have mastered the method after reading the case in this article. More exciting Please pay attention to other related articles on php Chinese website!

Recommended reading

How to use vue to request local json

##React props and state attribute practical case explanation


The above is the detailed content of How to get the uploaded image type and size. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template