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

原创 2007年09月28日 14:41:00

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

结果:

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

 


  
  

 

ASP.NET实现从服务器下载文件(记录)

假设在服务器的根目录下有个名为Download的文件夹,这个文件夹存放一些提供给引用程序下载的文件..     public void DownloadFile(string path, strin...

asp.net如何上传或下载文件到远程服务器

如何共享文件夹的话,文件夹必须有写入的权限,可是这样的话局域网内所有的人都可以上传到这个共享目录了? 建议: 这个我正好做过的,呵呵,给你参考 假设有服务器A是web服务器,文件要通过A...

ASP.net实现文件下载

方式一:TransmitFile实现下载。将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。     protected void Button1_Click(object se...

iOS开发之结合asp.net webservice实现文件上传下载(转)

作者:朱祁林 出处:http://zhuqil.cnblogs.com  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责...
  • joe1209
  • joe1209
  • 2012年06月19日 21:11
  • 689

iOS开发之结合asp.net webservice实现文件上传下载

iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下如何结合asp.net webservice实现文件上传下载。 首先,让我们看下文件下载。 这里我们下载cnblogs上的一个zip文件。使...

asp.net 实现文件下载

//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) ...{ ...

ASP .net 实现文件上传 下载功能

ASP .net 实现文件上传 下载功能   一、文件上传    1、单文件上传         利用FileUpload控件完成上传功能      代码:               ...
  • T_T_X_S
  • T_T_X_S
  • 2014年04月18日 16:03
  • 1008

ASP.NET中实现文件的保护性下载基础篇

http://www.jb51.net/article/26279.htm 许多时候,我们需要在因特网上提供文件下载服务,但是又要防止未经授权的下载,这时该怎么办?本文将为读者详细介绍一种使用A...

asp.net mvc如何实现文件下载

public Action LoadFile()         {             //   string filePath = Server.MapPath("~") + "upload/...

ASP.NET实现文件下载

http://blog.csdn.net/codeshark/article/details/2473664 方式一:TransmitFile实现下载。将指定的文件直接写入 HTTP...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:asp.net 实现下载服务器上的文件
举报原因:
原因补充:

(最多只允许输入30个字)