开发者

How to require FileUpload with actual upload, not just selecting a file

开发者 https://www.devze.com 2023-01-16 04:54 出处:网络
I have a FileUpload control along with a required field validator. It throws an error if the user d开发者_如何学Pythonoesn\'t click the Browse button to select a file (which is correct). However, if t

I have a FileUpload control along with a required field validator. It throws an error if the user d开发者_如何学Pythonoesn't click the Browse button to select a file (which is correct). However, if the user clicks the Browse button, but doesn't click the Upload button, ASP.NET's required validator doesn't throw an error. Any ideas how to fix?


Why not use a CustomValidator instead of a RequiredFieldValidator?

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = FileUpload1.PostedFile.ContentLength != 0;
    }

    private void Save()
    {
        if (Page.IsValid)
        {
            var myFileName = "somefile.jpg"
            FileUpload1.PostedFile.SaveAs(myFileName);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消