使用FileUpload,ListBox1控件实现图片的批量上传的整理

  1 //使用FileUpload,ListBox1控件实现图片的批量上传
  2 //原理:通过FileUpload控件向listbox中
  3 //添加图片(实际在listbox里显示的是图片在服务器中的路径)
  4 //点击上传后,图片会动态显示在listbox下方的div控件里,而且可为图片添加
  5 //标签,描述等  做的比较粗糙,还请各位大牛勿喷,有需要改进的地方请指正
  6 
  7 public partial class Control_批量上传 : System.Web.UI.UserControl
  8 {
  9     //Business层的类 usrInfo
 10     usrInfo oneUsr = new usrInfo();
 11     protected void Page_Load(object sender, EventArgs e)
 12     {
 13         //点击“上传”按钮 页面将会刷新,所以需要重新加载所有动态的控件
 14         //用ViewState来保持客户端和服务器端往返的状态
 15         if (ViewState["img0"] != null && (bool)ViewState["img0"])
 16         {
 17         //container 是页面div的ID
 18             container.Visible = false;
 19             CreateMyControls();
 20         } 
 21     }
 22     //向listbox 中添加图片(路径)
 23     protected void btn_add_Click(object sender, EventArgs e)
 24     {
 25         if (FileUpload1.HasFile)
 26         {
 27             string name = FileUpload1.FileName;
 28             string time = DateTime.Now.ToString("yyyymmddhhmmssffff");
 29             int n = name.LastIndexOf('.');  //查找name中‘.’的索引
 30              string suffix = name.Substring(n);
 31              //限制图片的格式
 32              if (suffix == ".jpg" || suffix == ".png" || suffix == ".bmp" || suffix == ".jpeg")
 33              {
 34                  string newName = name.Insert(n, time);
 35                  string path = Server.MapPath("~//picture//" + newName);
 36                  FileUpload1.SaveAs(path);
 37                  ListBox1.Items.Add(newName);
 38              }
 39              else
 40              {
 41                  Response.Write("<script>alert('暂不支持上传格式!')</script>");
 42              }
 43         }
 44     }
 45     
 46 
 47     private void CreateMyControls()
 48     {
 49         int i = 0;
 50         //动态添加控件
 51         foreach (ListItem ItemValue in ListBox1.Items)
 52         {
 53             Image img = new Image();
 54             //以下的Image,TextBox等控件之所以进行ID“有序”赋值,是因为上传到数据库的时候需要抓取这些html控件
 55             img.ID = "img" + i;
 56             ViewState["img" + i] = true;
 57             img.ImageUrl = "~//picture//" + ItemValue;
 58             img.Height = new Unit(100);
 59             img.BorderWidth = new Unit("0.2cm");
 60             img.Attributes.Add("onmouseout", "this.style.border='0.2cm solid gainsboro'");
 61             img.Attributes.Add("onmouseover", "this.style.border='0.2cm solid black'");
 62 
 63             TextBox txt_content = new TextBox();
 64             txt_content.ID = "txt_content" + i;
 65 
 66             ViewState["txt_content" + i] = true;
 67             txt_content.Text = "描述";
 68             txt_content.TextMode = TextBoxMode.MultiLine;
 69 
 70             TextBox txt_title = new TextBox();
 71             txt_title.ID = "txt_title" + i;
 72             ViewState["txt_title" + i] = true;
 73             txt_title.Text = "标题";
 74 
 75             TextBox txt_tags = new TextBox();
 76             txt_tags.ID = "txt_tags" + i;
 77             ViewState["txt_tags" + i] = true;
 78             txt_tags.Text = "标签";
 79 
 80             txt_title.Width = new Unit(180);
 81             txt_title.Height = new Unit(10);
 82             txt_tags.Width = new Unit(180);
 83             txt_tags.Height = new Unit(10);
 84             txt_content.Width = new Unit(180);
 85             txt_content.Height = new Unit(60);
 86 
 87             //使用asp:Literal控件显示Html代码
 88             Literal li1 = new Literal();
 89             Literal li2 = new Literal();
 90             Literal li3 = new Literal();
 91             Literal li4 = new Literal();
 92             Literal li5 = new Literal();
 93             
 94             string table1 = "<table style='width:360px;height:120px;text-align:center;display:inline-block;margin-bottom:50px'> <tr><td rowspan='3' style='height:120px'>";
 95             string table2 = "</td><td style='height:15px;width:200px'>";
 96             string table3 = " </td></tr><tr><td  style='height=15px;width:200px'>";
 97             string table4 = "</td></tr> <tr><td style='width:200px;height:100px'>";
 98             string table5 = "</td></tr> </table>";
 99             
100             li1.Text = table1;
101             li2.Text = table2;
102             li3.Text = table3;
103             li4.Text = table4;
104             li5.Text = table5;
105             
106             container.Controls.Add(li1);
107             container.Controls.Add(img);
108             container.Controls.Add(li2);
109             container.Controls.Add(txt_title);
110             container.Controls.Add(li3);
111             container.Controls.Add(txt_tags);
112             container.Controls.Add(li4);
113             container.Controls.Add(txt_content);
114             container.Controls.Add(li5);
115             i++;
116             if (i % 2 == 0)           //控制每行显示几个图片
117             {
118                 Literal li6 = new Literal();
119                 li6.Text = "<br />";
120                 container.Controls.Add(li6);
121             }
122         }
123         Session["num"] = i.ToString();
124     }
125     
126     //listbox 删除内容
127     protected void delete_Click(object sender, EventArgs e)
128     {
129         if (ListBox1.SelectedIndex < 0)
130             Response.Write("请选择要删除的选项");
131         else
132             ListBox1.Items.Remove(ListBox1.SelectedItem);
133     }
134 
135     protected void btn_upLoad_Click(object sender, EventArgs e)
136     {
137         int i = int.Parse(Session["num"].ToString());
138         //dropdownlist控件显示相册名,这里省略具体实现细节
139         string album_name=ddl_show_album.SelectedValue;
140         object obj = oneUsr.get_value_by_album_name(album_name, Session["user"].ToString());
141 
142             string album_id = obj.ToString();
143             //捕捉页面动态加载的控件,这也是上文为各个控件添加有序ID的原因了
144             for (int j = 0; j <= i - 1; j++)
145             {
146                 DateTime time = DateTime.Now;
147                 
148                 string url = (container.FindControl("img" + j) as Image).ImageUrl;
149 
150                 string txt_title = (container.FindControl("txt_title" + j) as TextBox).Text;
151 
152                 string txt_tags = (container.FindControl("txt_tags" + j) as TextBox).Text;
153 
154                 string txt_content = (container.FindControl("txt_content" + j) as TextBox).Text;
155                 //数据库插入操作
156                 oneUsr.pic_insert(url, time, txt_tags, txt_content, txt_title, album_id, url);
157             }
158             Response.Write("<script>alert('上传成功')</script>");
159     }   
160 }

转载于:https://www.cnblogs.com/dominix/archive/2012/10/28/2743954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值