gpt4 book ai didi

javascript - 不工作更正我的 javascript 代码以验证文件大小?

转载 作者:行者123 更新时间:2023-11-30 19:53:12 24 4
gpt4 key购买 nike

我用 php 和 javascript 设计了一个用于上传成员(member)照片的表单。

我有一个用于在服务器端验证上传表单的 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代码在验证图片大小功能时不能正常工作,并且在显示错误(警告)后不阻止文件上传?

最佳答案

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

关于javascript - 不工作更正我的 javascript 代码以验证文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54277630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com