asp.net读取与删除磁盘文件的实例代码

发布时间:2019-09-16编辑:脚本学堂
本文介绍下,使用asp.net代码读取与删除磁盘上文件的方法,举一个例子供大家学习参考。

分享一个实例:
asp.net读取磁盘文件、删除实例代码。

代码:
 

复制代码 代码示例:

protected void ReadFile() //读取文件夹,文件 
{
        string savePath = @"common";
        StringBuilder outstring = new StringBuilder();
        string absSavePath = Server.MapPath(savePath);
        string[] Directorys = Directory.GetDirectories(absSavePath + @"/" + Page.User.Identity.Name);
        //for (int i = 0; i < Directorys.Length; i++)
        //{
        //    Response.Write(Directorys[i] + " <br/ >");
        //}
        //Response.Write("Directorys count:" + Directorys.Length + "个  <br/ >--------- <br >");
        string[] Files = Directory.GetFiles(absSavePath + @"" + Page.User.Identity.Name, "*");
        outstring.Append("<ul>");
        for (int i = 0; i < Files.Length; i++)
        {
            if (Path.GetFileName(Files[i]) != "Thumbs.db")
            {  // www.jb200.com
                outstring.Append("<li><img src=/"common/" + Path.GetFileName(Files[i]) + "/" style=/"width:100px;height:100px;/" /><br/><input type=/"checkbox/" value=/"common/" + Path.GetFileName(Files[i]) + "/" name=/"picname/" onclick=/"getpicid();/" id=/"imgid" + i.ToString() + "/" /><label for=/"imgid" + i.ToString() + "/">" + Path.GetFileName(Files[i]) + "</label></li>");
            }
        }
        outstring.Append("</ul>");
        Response.Write(outstring.ToString());
    }

//删除文件 
string[] strpicname = Request["imgids"].Split(new char[] { ',' }); 
for (int i = 0; i < strpicname.Length; i++) 

    File.Delete(Server.MapPath(strpicname[i].ToString())); 
}

2,js代码
 

复制代码 代码示例:
//循环遍历复选框
<script language="javascript" type="text/javascript" defer="defer">
var imgnum = document.getElementsByTagName("input");
var imgids = "";
function getpicid()
{
  document.getElementById("hidimgid").value = "";
  for (var i=0;i<imgnum.length;i++)
  {
    if ( imgnum[i].type == "checkbox" && imgnum[i].name == "picname" && imgnum[i].checked)
    {
       imgids += imgids == "" ? imgnum[i].value : "," + imgnum[i].value;
   }
 }
 document.getElementById("hidimgid").value = imgids;
}
</script>