C# 如何在ASP.net AjaxFileUploader中验证文件大小

C# 如何在ASP.net AjaxFileUploader中验证文件大小,c#,asp.net,ajaxcontroltoolkit,C#,Asp.net,Ajaxcontroltoolkit,我正在使用AjaxFileUploader在我的ASP.net项目中上载多个文件 我想在将文件上载到4 MB之前验证文件大小 我的代码是: <ajaxToolkit:AjaxFileUpload id="ajaxUpload1" OnUploadComplete="ajaxUpload1_OnUploadComplete" ThrobberID="MyThrobber1" MaximumNumberOfFiles="5" runat="server" AllowedFil

我正在使用AjaxFileUploader在我的ASP.net项目中上载多个文件

我想在将文件上载到4 MB之前验证文件大小

我的代码是:

 <ajaxToolkit:AjaxFileUpload
    id="ajaxUpload1" OnUploadComplete="ajaxUpload1_OnUploadComplete"
    ThrobberID="MyThrobber1" MaximumNumberOfFiles="5" runat="server" AllowedFileTypes="swf,pdf"></ajaxToolkit:AjaxFileUpload>

    <asp:Image id="MyThrobber1" ImageUrl="~/images/loading.gif" Style="display:None" runat="server" />

请尝试以下操作:


我已经这样做了,如果文件大小超过4 MB,我想显示错误消息。基本上,最好在ACT项目中调整此文件,以便至少在支持HTML5的浏览器中验证客户端上的文件大小
  protected void ajaxUpload1_OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
    {

            int fileSize = e.FileSize;

                // fileSize in bytes
                if (fileSize < 4194304)
                {
                    //String currentDir = FileManager1.CurrentDirectory.PhysicalPath;
                    String FilePath = Path.Combine(currentDir, e.FileName);

                    // Save upload file to the file system
                    ajaxUpload1.SaveAs(FilePath);

                }

   }
protected void ajaxUpload1_OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
   try
   {
           int fileSize = e.FileSize;

     //String currentDir = FileManager1.CurrentDirectory.PhysicalPath;
      String FilePath = Path.Combine(currentDir, e.FileName);
            // fileSize in bytes
            if (fileSize > 4194304)
            {
             ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "SelectDetails", "alert('You Exceed the limit.');", true);    
              return;

            }
                // Save upload file to the file system
                ajaxUpload1.SaveAs(FilePath);
    }

 catch (Exception ex)    
  {
    throw ex;
  }

}