开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜