如何在文件上传中预先验证图像大小 [英] How to Pre validate Image size in fileupload

查看:72
本文介绍了如何在文件上传中预先验证图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个文件上传控件.我想要,当用户通过大小超过50 kb的控件浏览图像时,应该显示一条错误消息.该怎么做?
我有一个如下的customvalidator ...

Hi,
I have a fileupload control. I want, when a user browse a image through this control of size more than 50 kb , an error message should display. How to do this?
I have one customvalidator as below...

<asp:FileUpload ID="Fileupload1" runat="server"/>

<asp:CustomValidator ID="CustomValidator1"

                     runat="server"

                     ErrorMessage="not valid size"

                     Display="Dynamic"

                     ValidationGroup="I"

                     ControlToValidate="Fileupload1"

                     OnServerValidate="CustomValidator1_ServerValidate"

                     CssClass="validationCss">
</asp:CustomValidator>


protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Fileupload1.HasFile)
        {
            if (Fileupload1.PostedFile.ContentLength &amp;gt;= 20960)
            {
                args.IsValid = false;
               // this.ModalPopupExtender1.Show();
            }
            else
            {
                args.IsValid = true;
            }
        }
    }


我的验证正在运行,但是当我单击完成按钮时.
在单击按钮之前,我需要错误消息.
请帮忙.


My validation is working, but when i click on the done button.
I want error message before I click the button.
Please help.

推荐答案

您不需要为"Page_ClientValidate("groupName")编写代码
因为它是我们在页面上使用任何验证控件时预定义的
然后asp.net允许您使用此方法验证字段值....

您只需调用此函数即可..

希望你能理解..

因为我的英语不好..::(
you don''t need to write code for "Page_ClientValidate("groupName")"
because it''s predefined when we use any validation control on the page
then the asp.net allow you to validate field value using this method....

you just call this function it will work..

I hope you understand..

because my English is poor.. :(


嘿,你为什么不应该尝试javascript


Hey why should not you try javascript


<script type="text/javascript">
function OnBrowse()
{
if(Page_ClientValidate("I"))
{
///Your login here......
alert("something....");
return false;
}
return true;
}
</script>

调用此javascript函数以文件上传"onchange"事件....

call this javascript function to fileupload "onchange" event....


这篇关于如何在文件上传中预先验证图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆