[学习公共库]上传,删除图片(文件夹)代码 |
麦林 发表于 2007/6/21 17:26:45 | 源文件代码: <table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="border"> <tr valign="middle"> <td colspan="3" class="topbg" style="height: 22px"><div align="center"><strong>广告图片管理</strong></div></td> </tr> <tr class="tdbg"> <td height="20" colspan="3"><div align="center"> 添加广告图片: <asp:FileUpload ID="FileUpload1" runat="server" Width="266px" /> <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" ForeColor="Red" Width="153px"></asp:Label></div></td> </tr> </table>
代码.cs文件:上传: protected void Button1_Click(object sender, EventArgs e) { Boolean fileOK = false; string path = Server.MapPath("~/AdPic/"); string url = "../AdPic/" + FileUpload1.FileName; if (FileUpload1.HasFile) { string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName.ToLower()); string[] allowedExtentions = { ".gif", ".png", ".jpg", ".jpep" }; for (int i = 0; i < allowedExtentions.Length; i++) { if (fileExtension == allowedExtentions[i]) { fileOK = true; } } } if (fileOK) { FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName); //添加到硬盘上文件夹 string picname = FileUpload1.FileName; AddAdPic a = new AddAdPic(); a.InsertAdPic(picname); this.Label1.Text = "上传成功"; GetAllPic(); } else { this.Label1.Text = "不能上传此类型文件"; } }
删除: protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e) { int picid = int.Parse(e.Item.Cells[0].Text); string filename = e.Item.Cells[1].Text.ToString(); AddAdPic c = new AddAdPic(); try { c.DelAdPic(picid); System.IO.File.Delete(Server.MapPath("~/AdPic/") + filename); //硬盘上的删除 Response.Write("<script language=javascript>alert('删除成功!')</script>"); GetAllPic(); } catch { Response.Write("<script language=javascript>alert('删除失败!')</script>"); } }
|
|
|

.: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

blog名称:栗色?蓝色? 日志总数:449 评论数量:201 留言数量:37 访问次数:2260129 建立时间:2006年5月16日 |
|

.: 留言板
|

.: 链接
|

|