【问题标题】:dont work correct my javascript code for file size validation?不能更正我的 javascript 代码以进行文件大小验证?
【发布时间】:2019-06-14 02:45:21
【问题描述】:

我用php和javascript设计了一个上传会员照片的表单。

我有一个用于在服务器端验证上传表单的 PHP 代码,还有一个用于在客户端验证上传表单的代码。 PHP 代码工作正常,但 javascript 代码在检查文件大小和错误表示时会显示警报。如果文件大于 100 KB,它会发出警报,但不会停止上传文件。

javascript验证码是:

   <script language="javascript">
       function findSize()
       {
            var fileInput =  document.getElementById("fileup");
            try{
                var fsize=fileInput.files[0].size;
                var fsizekb1=fsize/1024;
                if(fsizekb1>100)
                {
                    //alert(fileInput.files[0].size+"vv"); // Size returned in bytes.
                    alert("your file is bigger than 100 KB and size of your file is "+fsizekb1.toFixed(2)+" KB");
                    return false;
                }
            }catch(e){

                var objFSO = new ActiveXObject("Scripting.FileSystemObject");
                var e = objFSO.getFile( fileInput.value);
                var fileSize = e.size;

                var fsizekb=filesize/1024;
                if(fsizekb>100)
                {
                    //alert(fileSize);
                    alert("your file is bigger than 100 KB and size of your file is "+fsizekb1.toFixed(2)+" KB");
                    return false;
                }
            }
            return true;
        }

    function up_control()
      {

            if(document.forms["frm_up"]["up"].value=='')
            {
            alert ("please select your photo by Browse");
            return false;  
            }
        if(document.forms["frm_up"]["cptch"].value=='')
            {
            alert ("please write the picture code");
            return false;
            }

            findsize();

      return true;
      }
    </script>

上传表格是这样的:

<form method="post" enctype="multipart/form-data" name="frm_up" onSubmit="return up_control()">
<input id="fileup" style=" margin-right:3%;" type="file" name="up" accept="image/jpeg,image/x-png,.jpg"  ><br> <br>


<!--===========start Area==Captcha====================================================================================
=====================================================================================================================
=====================================================================================================================
-->


    <img style="" HEIGHT="55px" src="cptch.php?cpf=<?php echo rand(1111,999999999); ?>" id='captchaimg' border="2" >&nbsp;&nbsp;&nbsp;&nbsp;<br>
    <label  for="message" style="margin-right:1%">write number in above picture:</label><br>
    <input style="width: 8em" id="cptch" name="cptch" type="text" autocomplete="off" ><br>
    <Label  style="margin-right:1%;" id="captex_chng_lbl">Can't Read the picture?  <br id="br_cap_chng"><a href='javascript: refreshCaptcha();' style="color:#909;" id="rmz2" class="captex_chng">Change</a></label><br><br>
    <script language='JavaScript' type='text/javascript'>
        function refreshCaptcha()
        {
            var img = document.images['captchaimg'];
            img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?cpf="+Math.random()*1000;
        }

    </script>

<!--===========END Area==Capcha======================================================================================
=====================================================================================================================
=====================================================================================================================
-->
<input type="hidden" name="upl" value="1">

<input style=" margin-right:3% !important;" type="submit" value="  Send to see " >&nbsp;<input type="button" style="background-color:#FF8080; margin-right:5%;"  value="&nbsp;&nbsp;&nbsp;cancel  &nbsp;&nbsp;&nbsp;" onClick="document.location='index.php?c=regm'" ><br><br>
</form> 

为什么javascript代码无法正常验证图像大小功能,并且显示错误(警报)后不阻止文件上传?

【问题讨论】:

  • 从您发布的代码来看,您的函数findSize() 根本不应该被执行,因为它似乎没有在任何地方被调用。您的函数up_control() 也在检查chk_fsize==2,但我看不到变量chk_fsize 被定义。这真的是与问题相关的所有代码,还是您删除了部分代码?
  • 另外,你为什么要在提交按钮上的onClick 中设置document.location?这段代码很混乱。
  • 现在您已经更改了代码。问题是您正在调用函数 findSize(); 但忽略响应。在您的up_control()-功能中,将最后一个return true; 更改为return findsize();
  • @MagnusEriksson 由于 php 代码,此 onclick 用于返回上一页,在这里并不重要

标签: javascript php forms validation


【解决方案1】:

首先,我感谢 MagnusEriksson 的良好指导。 但是最后一行是针对一切都正确,必须返回真值的时候,但是通过在up_control()中的findsize()函数之前添加return,问题就解决了。

【讨论】:

    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多