现在的位置: 首页 > 综合 > 正文

asp.net 2.0中判断上传的图片是否符合规格

2012年08月27日 ⁄ 综合 ⁄ 共 2713字 ⁄ 字号 评论关闭
  在asp.net 2.0中,在上传图片时,有时往往要判断用户上传的图片是否符合指定规格,如果不符合规格,
则不予以上传,那么可以用以下的代码片段进行判断。
    首先在web.config文件中设置图片上传的限制尺寸,比如
<appSettings>
  <add key="RequiredHeight" value="184"/>
  <add key="RequiredWidth" value="370"/>
</appSettings>

  之后,在页面获得web.config文件里预先设定的尺寸

if (Page.IsPostBack)
{
height = Convert.ToInt32(
ConfigurationManager.AppSettings.Get("RequiredHeight"));
width = Convert.ToInt32(
ConfigurationManager.AppSettings.Get("RequiredWidth"));
}

之后在“上传”按钮的事件代码中如下编写

    if (Page.IsValid)
    {
      if (FileUpload1.HasFile)
      {
        string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        switch (extension.ToLower())
        {
       case ".gif":
          case "jpg":
            try
            {
        //如果符合尺寸
              if (ValidateFileDimensions())
              {
                string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string saveAsName = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
                FileUpload1.PostedFile.SaveAs(saveAsName);
                lblSucces.Visible = true;
              }
              else
              {
                valInvalidDimensions.IsValid = false;
                valInvalidDimensions.ErrorMessage = String.Format(valInvalidDimensions.ErrorMessage, height, width);
              }
            }
            catch
            {
              // Unable to read the file dimensions. The uploaded file is probably not an image.
              valInvalidFile.IsValid = false;
            }
            break;

          default: // The uploaded file has an incorrect extension
            valInvalidFile.IsValid = false;
            break;
        }
      }
    }

   public bool ValidateFileDimensions()
  {
    using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
    {
      return (myImage.Height == height && myImage.Width == width);
    }
  }

要留意其中ValidateFileDimensions()中完成了判断尺寸的过程了
最后,页面放一些验证控件
<form id="form1" runat="server">
    <div>
      <asp:FileUpload ID="FileUpload1" runat="server" />
      <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload File" />
      <br />
      <asp:RequiredFieldValidator ID="valFileUpload1" ControlToValidate="FileUpload1" ErrorMessage="You must select a file first." runat="server" Display="Dynamic" />
      <asp:CustomValidator ID="valInvalidFile" runat="server" ErrorMessage="The file you uploaded doesn't appear to be a valid image." Display="Dynamic"></asp:CustomValidator>
      <asp:CustomValidator ID="valInvalidDimensions" runat="server" ErrorMessage="The image you uploaded has incorrect dimensions. Please select a file with a height of {0}px and a width of {1}px." Display="Dynamic" EnableViewState="false"></asp:CustomValidator>
      <asp:Label ID="lblSucces" runat="server" Text="The file you uploaded has been saved to disk successfully." Visible="false" EnableViewState="false"></asp:Label>
    </div>
  </form>

抱歉!评论已关闭.