ASP.NET用HttpListener實現檔案斷點續傳

來源:互聯網
上載者:User

標籤:des   blog   http   os   io   檔案   ar   art   問題   

本文轉載:http://www.cnblogs.com/TianFang/archive/2007/01/03/610739.html

斷點續傳的原理很簡單,就是在Http的請求和應答的報文頭上和一般的下載有所不同而已。普通方式請求伺服器上的一個文時,所發出的請求和接受到的伺服器如下:request header:Cache-Control: no-cacheConnection: closePragma: no-cacheAccept: */*Host: localhostresponse header:200Content-Type: application/octet-streamContent-Disposition: attachment;FileName=time.pdf當伺服器支援斷點續傳時,請求和應答如下:request header:Cache-Control: no-cacheConnection: closePragma: no-cacheAccept: */*Host: localhostRange: bytes=15360-response header:206Content-Type: application/octet-streamContent-Disposition: attachment;FileName=time.pdf兩個報文的不同部分已用紅色部分標記出來。可以看出:用戶端報文頭中通過Range報文頭來標識客戶期望的下載位置。伺服器的應答號為200時表示是從檔案頭開始下載,而206表示是從檔案的特定位置開始傳輸,用戶端從該應答號可以看出伺服器是否支援斷點續傳。也就是說,支援斷點續傳的時候可以從檔案任一部分開始下載,而普通的方式只能從檔案頭開始下載。要使得伺服器支援斷點續傳,需要解決以下幾個問題:1。需要判斷用戶端是否是續傳請求,如果是續傳請求時,需要擷取用戶端所需的檔案範圍。從上面的分析可以看到,當用戶端為斷點傳輸時,報文頭裡會增加Range欄位,則可以通過如下方式判斷是否是斷點傳輸請求。string range = request.Headers["Range"];bool isResume = string.IsNullOrEmpty(range);2。對用戶端做正確的應答相應,以通知用戶端伺服器支援端點續傳當為斷點傳輸請求時,對用戶端的相應號可以通過如下方式設定:response.StatusCode = 206;3。傳送用戶端所需正確的內容傳送用戶端所需正確的內容一般需要經過以下幾個步驟通過分析range來擷取用戶端的檔案請求範圍。斷點傳輸請求時,所需的長度比檔案的長度短,故需要正確的設定response.ContentLength64屬性。正確傳輸所需的內容程式碼範例:static void ProcessHttpClient(object obj){    HttpListenerContext context = obj as HttpListenerContext;    HttpListenerRequest request = context.Request;    HttpListenerResponse response = context.Response;    FileStream fs = File.OpenRead(@"f:\123.pdf"); //待下載的檔案    long startPos = 0;    string range = request.Headers["Range"];    bool isResume = string.IsNullOrEmpty(range);    if (isResume) //斷點續傳請求    {        //格式bytes=9216-        startPos = long.Parse(range.Split(‘=‘)[1].Split(‘-‘)[0]);        response.StatusCode = 206;        response.ContentLength64 = fs.Length - startPos;        fs.Position = startPos; //設定傳送的起始位置    }    else    {        response.ContentLength64 = fs.Length;    }    Console.WriteLine("request header");    Console.WriteLine(request.Headers.ToString());    response.ContentType = "application/octet-stream";    string fileName = "time.pdf";    response.AddHeader("Content-Disposition", "attachment;FileName=" + fileName);    Stream output = response.OutputStream;    try    {        Console.WriteLine("response header");        Console.WriteLine(response.Headers.ToString());        CopyStream(fs, output); //檔案傳輸        output.Close();    }    catch (HttpListenerException e) //在未寫完所有檔案時,如果用戶端關閉串連,會拋此異常    {        Console.WriteLine(e.Message);        //output.Close(); //如果執行此函數會拋異常在寫入所有位元組之前不能關閉流。    }}static void CopyStream(Stream orgStream, Stream desStream){    byte[] buffer = new byte[1024];    int read = 0;    while ((read = orgStream.Read(buffer, 0, 1024)) > 0)    {        desStream.Write(buffer, 0, read);        System.Threading.Thread.Sleep(1000); //類比慢速裝置    }}

  

ASP.NET用HttpListener實現檔案斷點續傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.