关闭

asp.net 实现下载服务器上的文件

762人阅读 评论(0) 收藏 举报

源代码:mycom.aspx.cs 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class mycom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] mydrives = System.IO.Directory.GetLogicalDrives();
            ArrayList drives = new ArrayList();
            for (int i = 0; i < mydrives.Length; i++)
            {
                drives.Add(mydrives[i]);

            }
            this.DropDownList1.DataSource = drives;
            this.DropDownList1.DataBind();
            ViewState["pf"] = this.DropDownList1.SelectedItem.Text;
            string pf = ViewState["pf"].ToString();
            createme(pf, ListBox1);
          
        }
       
       
   }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["pf"] = this.DropDownList1.SelectedValue;
        string pf = ViewState["pf"].ToString();
        createme(pf, ListBox1);
    
    }
    public string createme(string pf,ListBox listBox1)
    {
        this.ListBox1.Items.Clear();
       
        DirectoryInfo drive = new DirectoryInfo(pf);
        if (drive.Exists == false)
        {
            down(pf);
       
        }
        try
        {
            FileSystemInfo[] fileinfo = drive.GetFileSystemInfos();
      
        foreach (FileSystemInfo myfile in fileinfo)
        {
            this.ListBox1.Items.Add(myfile.FullName);
        }
        }
        catch
        { return "wrong!!"; }
        return "sussess";
      

      
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
      
            ViewState["pf"] = this.ListBox1.SelectedValue;
            string pf = ViewState["pf"].ToString();
            createme(pf, ListBox1);
     }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DirectoryInfo myinfo = new DirectoryInfo(this.ListBox1.SelectedValue);
        string my = myinfo.Parent.Parent.FullName;
        ViewState["pf"] = my;
        string pf = ViewState["pf"].ToString();
        createme(pf, ListBox1);
    }
    public void down(string pf)
    {
        FileStream r = new FileStream(pf, FileMode.Open);
        Response.Buffer = false;
        Response.AddHeader("Connection", "Keep-Alive");
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(pf));
        Response.AddHeader("Content-Length", r.Length.ToString());
        while (true)
        {
            byte[] buffer = new byte[1024];
            int leng = r.Read(buffer, 0, 1024);
            if (leng == 0)
            {
                break;
            }
            if (leng == 1024)
            {
                Response.BinaryWrite(buffer);
            }
            else
            {
                byte[] b = new byte[leng];
                for (int i = 0; i < leng; i++)
                    b[i] = buffer[i];
                Response.BinaryWrite(b);

           
            }
          
       
        }
     r.Close();
     Response.End();
    }
}

mycom.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mycom.aspx.cs" Inherits="mycom" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        &nbsp; &nbsp; &nbsp; &nbsp;
        <table style="width: 578px; height: 45px">
            <tr>
                <td colspan="3" style="width: 466px">
                    我的电脑:</td>
            </tr>
            <tr>
                <td colspan="3" rowspan="2" style="width: 466px; height: 21px">
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    </asp:DropDownList></td>
            </tr>
            <tr>
            </tr>
        </table>
        <table style="width: 573px; height: 460px">
            <tr>
                <td colspan="4" rowspan="3" style="height: 149px">
                    <asp:ListBox ID="ListBox1" runat="server" Height="451px" Width="503px" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>&nbsp;<br />
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="前进" />
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="后退" /></td>
            </tr>
            <tr>
            </tr>
            <tr>
            </tr>
        </table>
    </form>
</body>
</html>

结果:

在本机,调试成功,一般服务不允许访问逻辑盘上的内容>>>>>>>>

 


  
  

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1886次
    • 积分:41
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档