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 the file to download. String filePath = @ "D:\-ji-ai-nook. rar"; Or take the file name 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 (); Gets the size of the file long fileSize = Filestream.length; Long sum = 0; if (request.headers["Range"] = null) {Response.statuscode = 206;//indicates the HTTP output to return to the clientThe integer of the 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 ()); }//Get some HTTP header information Response.AddHeader ("Content-length", ((Long) (Filesize-sum)). ToString ()); Response.ContentType = "Application/octet-stream"; Get the file source Response.AddHeader ("content-disposition", "attachment; Filename= "+ Httputility.urlencode (Request.ContentEncoding.GetBytes (fileName)); Response.Flush (); filestream.position = sum; Sets the current stream position fileSize = filesize-sum; When the file size is greater than 0 is entered into the loop while (FileSize > 0) {//Determines whether the client is still connected in the server if (response.isclientconnected) {//Get buffer The total number of bytes. 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 {//when the user disconnects and exits the loop fileSize =-1; }}} catch (Exception ex) {Response.Write ("Error:" + ex.) Message); } finally {if (FileStream! = null) {//Close file Filestream.close (); } response.end (); } } }
ASP. NET breakpoint continued to pass