简体   繁体   English

如何要求FileUpload进行实际上传,而不仅仅是选择文件

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

I have a FileUpload control along with a required field validator. 我有一个FileUpload控件以及必需的字段验证器。 It throws an error if the user doesn'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. 但是,如果用户单击“浏览”按钮,但未单击“上载”按钮,则ASP.NET的必需验证器不会引发错误。 Any ideas how to fix? 任何想法如何解决?

Why not use a CustomValidator instead of a RequiredFieldValidator? 为什么不使用CustomValidator而不是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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM