实现多文件上传,文件下载,文件删除。公共类和文件上传页面的cs代码在文章后打包下载。 公共类代码
在文件上传页面(FileUp)中,页面代码如下。注意按钮不要放置在tabFU控件中,防止被clear掉。


<table style="width: 100%;" id="tabFU" runat="server"> 
<tr> 
<td> 
<asp:FileUpload ID="FileUpload1" runat="server" /> 
</td> 
</tr> 
</table> 
<asp:Button ID="btnUp" runat="server" Text="上传所有文件" onclick="btnUp_Click" /> 
<asp:Button ID="btnAddFU" runat="server" Text="增加上传文件" onclick="btnAddFU_Click" />

文件删除和下载cs代码:


/// <summary> 
    
/// 在文件夹Files下,删除指定的文件 
    
/// </summary> 
    
/// <param name="sqlStr">sql语句</param> 

    protected void DeleteTFN(string sqlStr) 
    

        SqlConnection myConn 
= CC.GetConnection(); 
        myConn.Open(); 
        SqlDataAdapter dapt 
= new SqlDataAdapter(sqlStr, myConn); 
        DataSet ds 
= new DataSet(); 
        dapt.Fill(ds, 
"files"); 
        
//获取指定文件的路径 
        string strFilePath = Server.MapPath("Files/"+ ds.Tables["files"].Rows[0][0].ToString(); 
        
//删除指定文件 
        File.Delete(strFilePath); 
        ds.Dispose(); 
        myConn.Close(); 
    }
 

    
/// <summary> 
    
/// 下载指定的文件 
    
/// </summary> 
    
/// <param name="strFilePath">文件在服务器上的路径</param> 

    protected void DownLoad(string strFilePath) 
    

        
if (File.Exists(strFilePath)) 
        

            System.IO.FileInfo file 
= new FileInfo(strFilePath); 
            
//清空缓冲区 
            Response.Clear(); 
            
//清空头 
            Response.ClearHeaders(); 
            Response.Buffer 
= true
            Response.ContentType 
= "application/pctet-stream"
            Response.AddHeader(
"Content-Disposition""attachment:filename=" + HttpUtility.UrlEncode(file.Name)); 
            Response.AppendHeader(
"Content-Length", file.Length.ToString()); 
            Response.WriteFile(file.FullName); 
            Response.Flush(); 
            Response.End(); 
        }
 
    }

只有注册用户登录后才能发表评论。


网站导航: