zoukankan      html  css  js  c++  java
  • Anthem.NET控件之FileUpload控件实现Ajax方式的文件上传

    Anthem.NET可以在此下载:http://sourceforge.net/project/showfiles.php?group_id=151897&package_id=168043&release_id=493609

    下载之后解压缩至硬盘中的某一目录中,编译项目得到Anthem.dll。然后将其拷贝到Web站点的bin目录下,添加引用。

    打开要使用这个控件的Web站点的Web.config文件,在<configuration>\ <system.web>\ <pages>\ <controls>中添加如下一行,注册Anthem.NET控件:

    1 <system.web>
    2     <pages>
    3       <controls>
    4         <add tagPrefix="anthem" namespace="Anthem" assembly="Anthem"/>
    5       </controls>
    6     </pages>

    Anthem.NET提供了一套自己就带有Ajax功能的、继承于现有ASP.NET控件的服务器端控件。根据上面在web.config文件中的注册,这部分控件的前缀为anthem。

    Anthem.NET的Ajax文件上传

    Anthem.NET中的Ajax文件上传功能靠的是其自己的FileUpload控件,其实使用起来和普通的ASP.NET FileUpload控件差不多,下面是HTML部分的代码:

    View Code
    1  <anthem:FileUpload ID="anthemFileUpload" runat="server" Width="185px" />
    2            <anthem:Button  ID="anthemUploadButton" runat="server" EnabledDuringCallBack="false" OnClick="anthemUploadButton_Click"
    3   Text="上传" Width="70px" TextDuringCallBack="上传中..." />  
    4             <anthem:Label ID="Label1" runat="server" Text=""></anthem:Label>
    5             <br />
    6             <anthem:Image ID="Image1" Visible="false" runat="server" />

    注意控件的前缀都是anthem。那个Button的TextDuringCallBack属性设置了异步回送时按钮中的文本;EnabledDuringCallBack属性让该按钮在进行异步回送时禁用,免得用户等得不耐烦。

    后台代码同样是将文件名和文件大小显示出来,不过注意这一句anthemResultLabel.UpdateAfterCallBack = true;,用来在回调之后更新anthemResultLabel上的文字:

    View Code
     1  protected void anthemUploadButton_Click(object sender, EventArgs e) //上传产品Logo
     2     {
     3         string path = Server.MapPath("~/ProductImg/");
     4         if (lblPic.Text != "")
     5         {
     6             if (System.IO.File.Exists(path + lblPic.Text))
     7             {
     8                 System.IO.File.Delete(path + lblPic.Text);
     9             }
    10         }
    11         bool fileOK = false;
    12         bool filesize = false;
    13         string fileExtension = System.IO.Path.GetExtension(anthemFileUpload.FileName).ToLower();
    14         string[] allowedExtensions = { ".gif", ".jpg", ".png", ".bmp", };
    15         for (int i = 0; i < allowedExtensions.Length; i++)
    16         {
    17             if (fileExtension == allowedExtensions[i])
    18             {
    19                 fileOK = true;
    20             }
    21         }
    22 
    23         if (anthemFileUpload.PostedFile.ContentLength > 1500000)
    24         {
    25             fileOK = false;
    26             filesize = true;
    27         }
    28 
    29         if (fileOK)
    30         {
    31             try
    32             {
    33                 Random RaName = new Random();
    34                 string RandomName = RaName.Next(100).ToString();
    35                 string PicFullName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + RandomName + fileExtension;
    36                 anthemFileUpload.SaveAs(path + PicFullName);
    37                 lblPic.Text = PicFullName;
    38                 Image1.Visible = true;
    39                 Image1.ImageUrl = "~/ProductImg/" + PicFullName;//在本页显示相片要用anthem控件
    40                 Image1.UpdateAfterCallBack = true;
    41                 Label1.Text = "上传成功!";
    42                 Label1.UpdateAfterCallBack = true;
    43             }
    44             finally
    45             {
    46             }
    47         }
    48         else
    49         {
    50             if (filesize)
    51             {
    52                 Label1.Text = "上传失败,上传图片文件小于150K";
    53                 Label1.UpdateAfterCallBack = true; 
    54             }
    55             else
    56             {
    57                 Label1.Text = "上传失败,只能上传:.jpg .bmp .gif .png的图片!";
    58                 Label1.UpdateAfterCallBack = true; 
    59             }
    60         }
    61     }   
  • 相关阅读:
    【javascript】ajax 基础
    【javascript】菜单滚动至顶部后固定
    【jquery】返回顶部效果
    【css】浅谈 inlineblock
    【jquery】checkbox——类似邮箱全选功能
    【javascript】返回顶部效果
    wcf基础知识之 查看soap消息 TcpTrace
    wcf系列之服务契约ServiceContract 之操作重载
    wcf 基础知识 之 消息交换模式 response/reply oneway deplex
    wcf基础知识之端口共享 portSharing
  • 原文地址:https://www.cnblogs.com/zwswood/p/2467797.html
Copyright © 2011-2022 走看看