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

源代码: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>

结果:

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

 


  
  

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值