欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-27 18:29:57
复制代码 代码如下:    protected void readfile()     //读取文件夹...

复制代码 代码如下:

    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")
            {
                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()));
        }


复制代码 代码如下:

//循环遍历复选框
<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>