Asp.net(c#)实现多线程断点续传

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gifSystem.IO.Stream iStream = null;
 2None.gif
 3None.gif            // Buffer to read 10K bytes in chunk:
 4None.gif            byte[] buffer = new Byte[10240];
 5None.gif
 6None.gif            // Length of the file:
 7None.gif            int length;
 8None.gif
 9None.gif            // Total bytes to read:
10None.gif            long dataToRead;
11None.gif
12None.gif            // Identify the file to download including its path.
13None.gif            string filepath  = @"E:\software\SQL Server 2000 Personal Edition.ISO";
14None.gif
15None.gif            // Identify the file name.
16None.gif            string  filename  = System.IO.Path.GetFileName(filepath);
17None.gif
18None.gif            try
19ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
20InBlock.gif                // Open the file.
21InBlock.gif                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 
22InBlock.gif                    System.IO.FileAccess.Read,System.IO.FileShare.Read);
23InBlock.gif                Response.Clear();
24InBlock.gif
25InBlock.gif                // Total bytes to read:
26InBlock.gif                dataToRead = iStream.Length;
27InBlock.gif
28InBlock.gif                long p = 0;
29InBlock.gif                if(Request.Headers["Range"]!=null)
30ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
31InBlock.gif                    Response.StatusCode = 206;
32InBlock.gif                    p = long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
33ExpandedSubBlockEnd.gif                }

34InBlock.gif                if(p != 0)
35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
36InBlock.gif                    Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());                    
37ExpandedSubBlockEnd.gif                }

38InBlock.gif                Response.AddHeader("Content-Length",((long)(dataToRead-p)).ToString());
39InBlock.gif                Response.ContentType = "application/octet-stream";
40InBlock.gif                Response.AddHeader("Content-Disposition""attachment; filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));
41InBlock.gif
42InBlock.gif                iStream.Position = p;
43InBlock.gif                dataToRead = dataToRead - p;
44InBlock.gif                // Read the bytes.
45InBlock.gif                while (dataToRead > 0)
46ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
47InBlock.gif                    // Verify that the client is connected.
48InBlock.gif                    if (Response.IsClientConnected) 
49ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
50InBlock.gif                        // Read the data in buffer.
51InBlock.gif                        length = iStream.Read(buffer, 010240);
52InBlock.gif
53InBlock.gif                        // Write the data to the current output stream.
54InBlock.gif                        Response.OutputStream.Write(buffer, 0, length);
55InBlock.gif
56InBlock.gif                        // Flush the data to the HTML output.
57InBlock.gif                        Response.Flush();
58InBlock.gif
59InBlock.gif                        buffer= new Byte[10240];
60InBlock.gif                        dataToRead = dataToRead - length;
61ExpandedSubBlockEnd.gif                    }

62InBlock.gif                    else
63ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
64InBlock.gif                        //prevent infinite loop if user disconnects
65InBlock.gif                        dataToRead = -1;
66ExpandedSubBlockEnd.gif                    }

67ExpandedSubBlockEnd.gif                }

68ExpandedBlockEnd.gif            }

69None.gif            catch (Exception ex) 
70ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
71InBlock.gif                // Trap the error, if any.
72InBlock.gif                Response.Write("Error : " + ex.Message);
73ExpandedBlockEnd.gif            }

74None.gif            finally
75ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
76InBlock.gif                if (iStream != null
77ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
78InBlock.gif                    //Close the file.
79InBlock.gif                    iStream.Close();
80ExpandedSubBlockEnd.gif                }

81InBlock.gif                   Response.End();
82ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/zhangchenliang/archive/2007/12/03/981239.html

猜你喜欢

转载自blog.csdn.net/weixin_33982670/article/details/93495949
今日推荐