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]; // Specify the path of the object to be downloaded. String filePath = @ "D: AI Zhiqiang .rar "; // Or get the file name including 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 (); // Obtain the file size Long fileSize = fileStream. Length; Long sum = 0; If (Request. Headers ["Range"]! = Null) { Response. StatusCode = 206; // an integer that indicates the HTTP output status 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 ()); } // Obtain some http header information Response. AddHeader ("Content-Length", (long) (fileSize-sum). ToString ()); Response. ContentType = "application/octet-stream "; // Obtain the File Source Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (Request. ContentEncoding. GetBytes (fileName ))); // Response. Flush (); FileStream. Position = sum; // you can specify the current stream Position. FileSize = fileSize-sum; // When the file size is greater than 0, it enters the loop While (fileSize> 0) { // Determine whether the client is still connected to the server If (Response. IsClientConnected) { // Obtain the total number of bytes in the buffer. Int length = fileStream. Read (buffer, 0, 10240 ); // Write data Response. OutputStream. Write (buffer, 0, length ); // Send the buffer output to the client Response. Flush (); Buffer = new Byte [10240]; FileSize = fileSize-length; } Else { // Exit the loop after 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 (); } } } |