asp.net (C #) implement multithread breakpoint continuation

Source: Internet
Author: User
Tags flush implement net range tostring
Asp.net| Multithreading

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 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's 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 ();
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.