本文介绍了在Asp.net中的File Uploader的finle类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制我的ClientS上传当前文件类型(如pdf)和文件大小?

how can i limit my ClientS to upload a Current file type (like "pdf") and file Size ?

推荐答案

protected void fileUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
      {
          string filePath = string.Empty;
          if (fileUpload.HasFile)
          {
              if (ValidFileType(fileUpload.FileName))
              {
                  bool saved = false;
                  try
                  {
                      filePath = ExternalAttachmentsFolder + KeyC();
                      if (filePath.Contains(@"\\"))
                      {
                          // unc
                          if (!Directory.Exists(filePath))
                          {
                              Directory.CreateDirectory(filePath);
                          }
                          filePath += fileUpload.FileName;
                          fileUpload.SaveAs(filePath);
                      }
                      else
                      {
                          if (!Directory.Exists(Server.MapPath(filePath)))
                          {
                              Directory.CreateDirectory(Server.MapPath(filePath));
                          }
                          filePath += fileUpload.FileName;
                          fileUpload.SaveAs(Server.MapPath(filePath));
                      }
                      saved = true;
                  }
                  catch (Exception ex)
                  {
                      fileUpload.FailedValidation = true;
                      Page.ClientScript.RegisterStartupScript(this.GetType(), "upfailed", "AnError('" + ex.Message + "');", true);
                  }
              }
              else
              {
                  // not valid file type
                  lblMessage.Text = LocalizationHelper.GetLocalizedName("FileTypeNotAllowed", Localization.GetCurrentCultureID());
                  fileUpload.FailedValidation = true;
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "upfailed", "AnError('" + lblMessage.Text + "');", true);
              }
          }
      }


 private bool ValidFileType(string filename)
      {
          bool valid = false;

       valid = fileName.EndsWith(".txt");
          return valid;
      }


这篇关于在Asp.net中的File Uploader的finle类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:21