Here I add an HTTP header to the output stream through the AddHeader method in the response class. In the HTTP header, it consists of the header and the body information. The two lines are separated by a blank line. This uses a range segment in the header to indicate where the client wants to continue downloading to implement the Resume function.
Okay, no more nonsense, let's get started.
1. New 1 homepage, name casually up ha.
2. Add 1 LinkButton buttons to the page to perform the implementation process.
3. In the LinkButton Click event, the implementation of the breakpoint continued to pass function.
The code is as follows:
Also do not forget to reference System.IO namespace, here only post the backstage implementation code (the front desk will not be able to go back from learning ...) )
Copy Code code as follows:
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 a bit array
byte[] buffer = new byte[10240];
Specifies the path to download the file.
String filePath = @ "D:\ love the Wisdom of the corner. RAR";
or take the filename including the extension
String fileName = Path.getfilename (FilePath);
Stream fileStream = null;
Try
{
Open File
FileStream = new FileStream (FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
Response.Clear ();
Get the size of a file
Long fileSize = filestream.length;
Long sum = 0;
if (request.headers["Range"]!= null)
{
Response.statuscode = 206; An integer that represents the state of the HTTP output returned to the client. 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 ());
}
Get Partial 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; Set Current stream location
FileSize = Filesize-sum;
When the file size is greater than 0 is into the loop
while (FileSize > 0)
{
To determine 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 loop when user disconnects
FileSize =-1;
}
}
}
catch (Exception ex)
{
Response.Write ("Error:" + ex.) message);
}
Finally
{
if (FileStream!= null)
{
Close File
Filestream.close ();
}
Response.End ();
}
}
}
This is relatively simple, please make appropriate changes according to the actual situation.