ASP.NET file breakpoint continuation implementation code

  • 2020-05-17 05:10:29
  • OfStack

Here I add an HTTP header to the output stream through the AddHeader method in the Response class. In the HTTP header, it is composed of header information and body information. The two are separated by a blank line. Here, Range is added to the header to indicate where the client wants to continue the download to achieve the continuation function.
Well, without further ado, let's get started.
1. Create a new homepage with a random name.
2. Add an LinkButton button to the page to perform the implementation.
3. In the Click event of LinkButton, the breakpoint continuation function is implemented.
The code is as follows:
Also don't forget to refer to the System.IO namespace, where only the background implementation code is posted.
 
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.IO; 
public partial class DFile : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
} 
protected void LinBtnDFile_Click(object sender, EventArgs e) 
{ 
//  create 1 Bit array  
byte[] buffer = new Byte[10240]; 
//  Specify the path to download the file . 
string filePath = @"D:\ Love wisdom from .rar"; 
//  Or take the filename to include the extension  
string fileName = Path.GetFileName(filePath); 
Stream fileStream = null; 
try 
{ 
//  Open the file  
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 
Response.Clear(); 
//  Gets the size of the file  
long fileSize = fileStream.Length; 
long sum = 0; 
if (Request.Headers["Range"] != null) 
{ 
Response.StatusCode = 206; //  Represents a return to the client  HTTP  The integer of the output state. The default value is  200 .  
sum = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Replace("-", "")); 
} 
if (sum != 0) 
{ 
Response.AddHeader("Content-Range", "bytes " + sum.ToString() + "-" + ((long)(fileSize)).ToString() + "/" + fileSize.ToString()); 
} 
//  To get some http Header information  
Response.AddHeader("Content-Length", ((long)(fileSize - sum)).ToString()); 
Response.ContentType = "application/octet-stream"; 
// Get file source  
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileName))); 
// Response.Flush(); 
fileStream.Position = sum; // Sets the current flow position  
fileSize = fileSize - sum; 
//  When the file size is greater than 0 Is going into the loop  
while (fileSize > 0) 
{ 
//  Determines whether the client is still connected to the server  
if (Response.IsClientConnected) 
{ 
//  Gets the total number of bytes in the buffer . 
int length = fileStream.Read(buffer, 0, 10240); 
//  Write data  
Response.OutputStream.Write(buffer, 0, length); 
//  Sends the output of the buffer to the client  
Response.Flush(); 
buffer = new Byte[10240]; 
fileSize = fileSize - length; 
} 
else 
{ 
// Exit the loop when the user is disconnected  
fileSize = -1; 
} 
} 
} 
catch (Exception ex) 
{ 
Response.Write("Error : " + ex.Message); 
} 
finally 
{ 
if (fileStream != null) 
{ 
// Close the file  
fileStream.Close(); 
} 
Response.End(); 
} 
} 
} 

Here is relatively simple, please make appropriate modifications according to the actual situation.

Related articles: