文件上传及删除
文件上传及删除
相关文件:生成缩略图
上传:
.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" onclick="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>");
            }
}
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号 
