文件上传及删除
文件上傳及刪除
相關文件:生成縮略圖
?上傳:
.aspx:
<input id="picFile1" runat="server" name="picFile" type="file" /><asp:Button ID="picUp"? runat="server" Text="上傳" OnClick="picUp_Click"? /><asp:Label ID="Label1" runat="server" Text="上傳成功" Visible="false"? ForeColor="Red"></asp:Label>
<div runat="server" id="dvLun" style="display: none">
??????????????????????????????????????????????????? <asp:Image ID="Image1" runat="server" Width="101px" Height="70px" /><%-- Width="280px" Height="220px"--%>
??????????????????????????????????????????????? </div>
?
.aspx.cs:
if (picFile1.PostedFile.ContentLength != 0)
??????????? {
??????????????? //當前日期 加/? 例子: 2009-6-25/
??????????????? string strPath = DateTime.Now.ToShortDateString() + "/";
??????????????? //文件名加后綴名???????????
??????????????? string fileName = picFile1.PostedFile.FileName.Substring(picFile1.PostedFile.FileName.LastIndexOf('\\') + 1);
??? //擴展綴名
??? //string exName = System.IO.Path.GetExtension(fileName);
??????????????? // 得到跟目錄 /sjzSaveHouTai
??????????????? string virtualPath = Page.Request.ApplicationPath;
??????????????? //
??????????????? //string mPath = System.Web.HttpContext.Current.Request.MapPath("Uploads/" + strPath);
??????????????? string realPath = Server.MapPath("http://www.cnblogs.com/Uploads/picScroll/");//+ strPath
??????????????? if (!System.IO.Directory.Exists(realPath))
??????????????? {
??????????????????? System.IO.Directory.CreateDirectory(realPath);
??????????????? }
??????????????? try
??????????????? {
??????????????????? //字節大小
??????????????????? int size = picFile1.PostedFile.ContentLength;
??????????????????? //獲得后綴
??????????????????? string exName = picFile1.PostedFile.FileName.Substring(picFile1.PostedFile.FileName.LastIndexOf(".") + 1).ToLower();
//string exName = System.IO.Path.GetExtension(fileName);
??????????????????? //判斷擴展名
??????????????????? if (exName != "jpg" && exName != "jpeg" && exName != "bmp" && exName != "gif")
??????????????????? {
??????????????????????? ClientScript.RegisterStartupScript(GetType(), "", "<script type='text/javascript'>alert('禁止上傳非圖片格式!');</script>"); return;
??????????????????? }
//生成文件名? 另:如果批量上傳,不要用此名,因為時間非常短,在同一秒鐘的文件名都是一樣的,根本無法區別.
??????????????????? string fn = System.IO.Path.GetFileName(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
?????????????????????? DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "." + exName);
??????????????????? picFile1.PostedFile.SaveAs(realPath + fn);
??????????????????? //picFile1.PostedFile.SaveAs(realPath + fileName);
??????????????????? //imguf.Src = "Uploads/" + fn;
???????????????????
??????????????????? //文件路徑
??????????????????? ViewState[picFile1.ID] = "Uploads/picScroll/"+ strPath+ fn;//
??????????????????? ViewState[picFile1.ID+"size"] = size;
??????????????????? //ViewState["picexName"] = exName;
//alVeiwNum用來存儲批量上傳時的文件路徑,此為ArrayList型的public static的實例
??????????????????? //alVeiwNum.Add("Uploads/picScroll/" + fileName);
??????
??????????????????? picUp.Enabled = false;
??????????????????? Label1.Visible = true;
Label1.Text = "上傳成功";
??????????????????? //AddAcc.Enabled = false;
??????????????????? if (list.Count ==1)
??????????????????? {
??????????????????????? dvLun.Style.Add("display", "block");
??????????????????????? Image1.ImageUrl = "http://www.cnblogs.com/" + ViewState[picFile1.ID].ToString();
??????????????????? }
??????????????? }
??????????????? catch (Exception err)
??????????????? {
??????????????????? Response.Write(err.ToString());
??????????????? }
??????????? }
?
?刪除:
.aspx:
表頭模板
<input id="chk" type="checkbox" οnclick="selAll()">全選</input>
<script type="text/javascript">
??????? function selAll() {
??????????? if (form1.chk.checked) {
??????????????? for (var i = 0; i < document.form1.elements.length; i++) {
??????????????????? var e = document.form1.elements[i];
??????????????????? if (e.name.indexOf("checkid") != -1) e.checked = true;
??????????????? }
??????????? }
??????????? else {
??????????????? for (var i = 0; i < document.form1.elements.length; i++) {
??????????????????? var e = document.form1.elements[i];
??????????????????? if (e.name.indexOf("checkid") != -1) e.checked = false;
??????????????? }
??????????? }
??????? }
<script>
模板列里
<input name="checkid" type="checkbox" value='<%# Eval("id") %>'>
.aspx.cs
刪除按鈕事件
?
?if (Request.Form["checkid"] != null)
??????? {
??????????? string sql = "delete from 表名 where id in (" + Request.Form["checkid"] + "); ";
??????????? try
??????????? {
??????????????? szzf.BLL.picScroll bPS = new szzf.BLL.picScroll();
??????????????? List<szzf.Model.picScroll> list = bPS.GetModelList("psid in (" + Request.Form["checkid"]+")");
???????????????
??????????????? for (int i = 0; i < list.Count;i++ )
??????????????? {
??????????????????? int iIndex = list[i].psUrl.LastIndexOf('/');
??????????????????? string url? = list[i].psUrl;
??????????????????? string stFilename = url.Substring(iIndex + 1);
//這里應為要刪除文件路徑,否則拋出異常
??????????????????? string realPath = Server.MapPath("http://www.cnblogs.com/"+list[i].psUrl);
??????????????????? File.Delete(realPath);
???????????????????
??????????????? }
??????????????? int num = Maticsoft.DBUtility.DbHelperSQL.ExecuteSql(sql);
??????????????? init();
??????????? }
??????????? catch (System.Exception ex)
??????????? {
??????????????? ClientScript.RegisterStartupScript(GetType(), "", "<script type='text/javascript'>alert('');</script>");
??????????? }
??????? }
?
?
轉載于:https://www.cnblogs.com/sgivee/archive/2010/07/18/1780229.html
總結
- 上一篇: 虚拟化部署之Hyper-V虚拟网络配置
- 下一篇: mysql中的复制(配置主从数据库)