1. 程式人生 > >asp.net 用流的方式下載檔案

asp.net 用流的方式下載檔案

//以下程式碼根據別人文章和自己整理  
 
 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.IO;  
 
namespace WebApplication1  
{  
    public partial class DownfFile : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
             
        }  
        /// <summary>  
        /// WriteFile下載  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            string name ="DreamweaverCS3簡體中文免啟用綠色版.rar";  
            string path =Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar");  
            FileInfo fileinfo = new FileInfo(path);  
            long size = fileinfo.Length;  
            Response.Clear();  
            Response.ContentType = "application/octet-stream";  
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + name);  
            Response.AppendHeader("Content-Length", size.ToString());  
            Response.AppendHeader("Content-Transfer-Encoding", "binary");  
            Response.ContentEncoding = System.Text.Encoding.UTF8;//GetEncoding("gb2312");  
            Response.WriteFile(path,0,size);  
            Response.Flush();  
            Response.End();  
        }  
        /// <summary>  
        /// 使用OutputStream.Write分塊下載檔案  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
     
        protected void Button2_Click(object sender, EventArgs e)  
        {  
            string fileName = HttpContext.Current.Server.UrlEncode("DreamweaverCS3簡體中文免啟用綠色版.rar");  
            string filePath = HttpContext.Current.Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar");  
            //如果要寫類的話用HttpResponse ht=Page.Response然後方法寫void DownloadFile(HttpResponse response, string serverPath)  
 
 
            FileInfo fileinfo = new FileInfo(filePath);  
            if (fileinfo.Exists)  
            {  
               const long size = 102400;         //指定下載塊的大小  
                byte[] by = new byte[size]; //建立一個100kb的快取去大小  
                long dataread = 0;          //已讀的位元組數  
                try 
                {  
                    //開啟檔案    
                    FileStream filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read);  
                    dataread = filestream.Length;//檔案總的大小  
                    int i = 0;  
                    //新增Http頭  
                    Response.Clear();  
                    Response.ContentType = "application/octet-stream";  
                    Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);  
                    Response.AddHeader("Content-Length", dataread.ToString());  
                    while (dataread > 0)  
                    {  
                        if (Response.IsClientConnected)  
                        {  
                            int length = filestream.Read(by,0, Convert.ToInt32(size));  
                            Response.OutputStream.Write(by, 0, length);  
                            Response.Flush();  
                            Response.Clear();  
                            by = new byte[size];  
                            i++;  
                            dataread = dataread - length;//判斷是否讀取玩 如果讀取完就跳出while迴圈  
                        }  
                        else 
                        {  
                            dataread = -1;//客戶端已經失去連線中段操作  
                        }  
                        //filestream.Close();  
                        //Response.Close();就是多寫這行程式碼 導致下載檔案只有100kb  
                    }  
                }  
                catch  { }  
 
            }  
        }  
          
        /// <summary>  
        /// 使用BinaryWrite下載檔案,大檔案效率不行  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button3_Click(object sender, EventArgs e)  
        {  
            string name = "DreamweaverCS3簡體中文免啟用綠色版.rar";  
            string path = Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar");  
 
            FileStream filestream = File.OpenRead(path);  
            byte[] by = new byte[filestream.Length];  
 
            filestream.Read(by, 0, by.Length);  
            filestream.Close();  
            Response.ContentType = "application/octet-stream";  
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name));  
            Response.AddHeader("Content-Length", by.Length.ToString());  
            Response.BinaryWrite(by);  
            Response.Flush();  
            Response.End();  
        }  
        /// <summary>  
        /// 使用TransmifFile下載檔案  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button4_Click(object sender, EventArgs e)  
        {    
            //提供下載的檔案,不編碼的話檔名會亂碼     
            string fileName = "DreamweaverCS3簡體中文免啟用綠色版.rar"; //HttpContext.Current.Server.UrlEncode("DreamweaverCS3簡體中文免啟用綠色版.rar");  
             string filePath = Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar");//HttpContext.Current.Server.UrlEncode(Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar"));  
             FileInfo fileinfo = new FileInfo(filePath);  
             long filesize = fileinfo.Length;  
             Response.Clear();  
             Response.ContentType = "application/x-zip-compressed";  
             Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName);  
            //不指明Content-Length用Flush的話不會顯示下載進度  
             Response.AddHeader("Content-Length", filesize.ToString());  
             Response.TransmitFile(fileName, 0, filesize);  
             Response.Flush();  
             Response.Close();  
        }  
        /// <summary>  
        /// 使用BinaryWrite分塊下載檔案  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button5_Click(object sender, EventArgs e)  
        {  
            string fileName = HttpContext.Current.Server.UrlEncode("DreamweaverCS3簡體中文免啟用綠色版.rar");  
            string filePath = HttpContext.Current.Server.MapPath("DreamweaverCS3簡體中文免啟用綠色版.rar");  
 
            long size = 102400;         //指定下載塊的大小  
            byte[] by = new byte[size]; //建立一個100kb的快取去大小  
            long dataread = 0;          //已讀的位元組數  
            try 
            {  
                //開啟檔案    
                FileStream filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read);  
                dataread = filestream.Length;//檔案總的大小  
 
                //新增Http頭  
                Response.Clear();  
                Response.ContentType = "application/octet-stream";  
                Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);  
                Response.AddHeader("Content-Length", dataread.ToString());  
                while (dataread > 0)  
                {  
                    if (Response.IsClientConnected)  
                    {  
                        int length = filestream.Read(by, 0, Convert.ToInt32(size));  
                        Response.BinaryWrite(by);  
                        Response.Flush();  
                        Response.Clear();  
                        dataread = dataread - length;//判斷是否讀取玩 如果讀取完就跳出while迴圈  
                    }  
                    else 
                    {  
                        dataread = -1;//客戶端已經失去連線中段操作  
                    }  
                    //filestream.Close();  
                    //Response.Close();  
                }  
            }  
            catch  { }  
        }  
          
    }