散分顺便问一个ASP.NET大文件下载的问题。总的来说我只知道三种方法,一个是直接用超连接绑定到给文件的路径,第二个是Response.WriteFile Response.TransmitFile 第三个是System.IO.Stream iStream = null;// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];// Length of the file:
int length;// Total bytes to read:
long dataToRead;// Identify the file to download including its path.
string filepath = "DownloadFileName";// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);try {
    // Open the file.
    iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
    System.IO.FileAccess.Read, System.IO.FileShare.Read);
    // Total bytes to read:
    dataToRead = iStream.Length;    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);    // Read the bytes.
    while (dataToRead > 0) {
        // Verify that the client is connected.
        if (Response.IsClientConnected) {
            // Read the data in buffer.
            length = iStream.Read(buffer, 0, 10000);            // Write the data to the current output stream.
            Response.OutputStream.Write(buffer, 0, length);            // Flush the data to the HTML output.
            Response.Flush();            buffer = new Byte[10000];
            dataToRead = dataToRead - length;
        } else {
            //prevent infinite loop if user disconnects
            dataToRead = -1;
        }
    }
} catch (Exception ex) {
    // Trap the error, if any.
    Response.Write("Error : " + ex.Message);
} finally {
    if (iStream != null) {
        //Close the file.
        iStream.Close();
    }
}
下面说下个人的看法,不对的地方大家指出来
我感觉第一个方法缺点是  无法在程序中控制下载权限,无法统计下载信息,第二个下载方法貌似说下载大文件时候很占系统内存。第三个方法貌似是解决了第二个方法的不足,但是貌似不支持第三方软件的下载如迅雷。  请各位DX说下有什么好一点的方法没、有没有  能够控制下载权限 统计下载信息 下载大文件的时候不会出这样那样的问题 然后能够用下载软件下载的方法没。
最后在祝大家新年快乐,在新的一年里心想事成。。

解决方案 »

  1.   

    可以写客户端FTP的操作类,这样支持第三方软件
      

  2.   

    我觉得你去研究一下HTTP协议,就能很好的解答这个问题了。我不知道你所说的不支持下载工具是指什么,是不是不支持断点续传。如果是的话,去看看HTTP的206 Partial Response就行了,里面说明了如何在Request中声明请求文件的一个部分,如何在Response中声明返回的就是这个部分,然后改进方法3来实现206就可以了。