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

简介:
System.IO.Stream iStream = null; 

// Buffer to read 10K bytes in chunk: 
byte[] buffer = new Byte[10240]; 

// Length of the file: 
int length; 

// Total bytes to read: 
long dataToRead; 

// Identify the file to download including its path. 
string filepath = @”E:\software\SQL Server 2000 Personal Edition.ISO”; 

// 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); 
Response.Clear(); 

// Total bytes to read: 
dataToRead = iStream.Length; 

long p = 0; 
if(Request.Headers["Range"]!=null) 

Response.StatusCode = 206; 
p = long.Parse( Request.Headers["Range"].Replace(”bytes=”,”").Replace(”-”,”")); 

if(p != 0) 

Response.AddHeader(”Content-Range”,”bytes ” + p.ToString() + “-” + ((long)(dataToRead - 1)).ToString() + “/” + dataToRead.ToString()); 

Response.AddHeader(”Content-Length”,((long)(dataToRead-p)).ToString()); 
Response.ContentType = “application/octet-stream”; 
Response.AddHeader(”Content-Disposition”, “attachment; filename=” + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename))); 

iStream.Position = p; 
dataToRead = dataToRead - p; 
// 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, 10240); 

// 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[10240]; 
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(); 

Response.End(); 

}

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1081571


相关文章
|
2月前
|
数据采集 XML JavaScript
C# 中 ScrapySharp 的多线程下载策略
C# 中 ScrapySharp 的多线程下载策略
|
15天前
|
安全 数据库连接 API
C#一分钟浅谈:多线程编程入门
在现代软件开发中,多线程编程对于提升程序响应性和执行效率至关重要。本文从基础概念入手,详细探讨了C#中的多线程技术,包括线程创建、管理及常见问题的解决策略,如线程安全、死锁和资源泄露等,并通过具体示例帮助读者理解和应用这些技巧,适合初学者快速掌握C#多线程编程。
49 0
|
2月前
|
安全 C# 开发者
【C# 多线程编程陷阱揭秘】:小心!那些让你的程序瞬间崩溃的多线程数据同步异常问题,看完这篇你就能轻松应对!
【8月更文挑战第18天】多线程编程对现代软件开发至关重要,特别是在追求高性能和响应性方面。然而,它也带来了数据同步异常等挑战。本文通过一个简单的计数器示例展示了当多个线程无序地访问共享资源时可能出现的问题,并介绍了如何使用 `lock` 语句来确保线程安全。此外,还提到了其他同步工具如 `Monitor` 和 `Semaphore`,帮助开发者实现更高效的数据同步策略,以达到既保证数据一致性又维持良好性能的目标。
32 0
|
4月前
|
开发框架 监控 Java
【.NET Core】多线程之线程池(ThreadPool)详解(二)
【.NET Core】多线程之线程池(ThreadPool)详解(二)
79 3
|
4月前
|
SQL 开发框架 Java
【.NET Core】多线程之线程池(ThreadPool)详解(一)
【.NET Core】多线程之线程池(ThreadPool)详解(一)
255 2
|
4月前
|
大数据 C#
C#实现多线程的几种方式
C#实现多线程的几种方式
|
4月前
|
算法 安全 Java
【.NET Core】 多线程之(Thread)详解
【.NET Core】 多线程之(Thread)详解
50 1
|
27天前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
27 7
|
25天前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
38 0