用户选择文件时检查图像文件大小的代码是什么

时间:2018-06-02 20:02:01

标签: javascript

在用户选择文件时,要添加到下面的代码以检查图像文件大小的代码是什么?如果文件大小超过5MB,则提供警报?它应首先检查文件扩展名,如果文件扩展名是好的,则应检查大小。

<script type="text/javascript">
        /*<![CDATA[*/
        $("#ProductImage").change(function() {
            var f = this.value;
            var e = f.replace(/^.*\\/, "");
            var d = e.split(".").pop();
            if (d == "jpg" || d == "jpeg" || d == "png" || d == "gif" || d == "JPG" || d == "JPEG" || d == "PNG" || d == "GIF") {
                $("#file_name").val(e)
            } else {
                alert("Please upload a valid image file.")
            }
        });

</script>

2 个答案:

答案 0 :(得分:1)

f.size将以字节为单位获取文件的大小, 5242880是5mb字节

if (f.size >= 5242880){ alert("Image file is too big)}

有关file.size&gt;的更多信息https://www.w3schools.com/jsref/prop_fileupload_files.asp

答案 1 :(得分:0)

您好请查看以下代码

function validateFileExtension(component,msg_id,msg,extns)
{
   var flag=0;
   with(component)
   {
      var ext=value.substring(value.lastIndexOf('.')+1);
      for(i=0;i<extns.length;i++)
      {
         if(ext==extns[i])
         {
            flag=0;
            break;
         }
         else
         {
            flag=1;
         }
      }
      if(flag!=0)
      {
         document.getElementById(msg_id).innerHTML=msg;
         component.value="";
         component.style.backgroundColor="#eab1b1";
         component.style.border="thin solid #000000";
         component.focus();
         return false;
      }
      else
      {
         return true;
      }
   }
}

function validateFileSize(component,maxSize,msg_id,msg)
{
   if(navigator.appName=="Microsoft Internet Explorer")
   {
      if(component.value)
      {
         var oas=new ActiveXObject("Scripting.FileSystemObject");
         var e=oas.getFile(component.value);
         var size=e.size;
      }
   }
   else
   {
      if(component.files[0]!=undefined)
      {
         size = component.files[0].size;
      }
   }
   if(size!=undefined && size>maxSize)
   {
      document.getElementById(msg_id).innerHTML=msg;
      component.value="";
      component.style.backgroundColor="#eab1b1";
      component.style.border="thin solid #000000";
      component.focus();
      return false;
   }
   else
   {
      return true;
   }
}

还要检查以下代码

function checkFile(e) {

    /// get list of files
    var file_list = e.target.files;

    /// go through the list of files
    for (var i = 0, file; file = file_list[i]; i++) {

        var sFileName = file.name;
        var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
        var iFileSize = file.size;
        var iConvert = (file.size / 1048576).toFixed(2);

        /// OR together the accepted extensions and NOT it. Then OR the size cond.
        /// It's easier to see this way, but just a suggestion - no requirement.
        if (!(sFileExtension === "pdf" ||
              sFileExtension === "doc" ||
              sFileExtension === "docx") || iFileSize > 10485760) { /// 10 mb
            txt = "File type : " + sFileExtension + "\n\n";
            txt += "Size: " + iConvert + " MB \n\n";
            txt += "Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
            alert(txt);
        }
    }
}