C # Implement large file block sends to the client

xiaoxiao2021-03-06  67

System.io.stream istream = NULL;

// buffer to read 10k bytes in chunk: byte [] buffer = new byte [10000];

// Length of the file: int layth;

// Total Bytes to read: long datatoread;

// Identify the file to download incruding its path. String filepath = "DownloadFileName";

// 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);

// Total Bytes to read: datatoread = istream.length;

Response.contentType = "Application / Oct-stream"; response.addheader ("Content-Disposition", "Attachment; FileName =" FileName);

// read the bytes. While (datatoread> 0) {// Verify That the client is connection. If (response.isclientconnected) {// read the data in buffer. Length = istream.read (buffer, 0, 1000);

// 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 [10000]; datatoread = datatoread - length;} else {// prevent infinite loop if user disponnects dattoread = -1;}}} catch (exception ex) {// trap the error, if any. response. Write ("Error:" EX.MESSAGE);} finally {if (iStream! = Null) {// close the file. Istream.close ();}}

转载请注明原文地址:https://www.9cbs.com/read-90510.html

New Post(0)