我使用Asp.net,.net 3.5,win2003,iis 6.0
我想在我的应用程序中:
从Oracle读取DATA
使用FileStream和BZip2解压缩文件
当我从磁盘读取文件,失败!!!并获得OutOfMemory …
.我的代码是:
using (var fs3 = new FileStream(filePath2,FileMode.Open,FileAccess.Read)) { byte[] b2 = ReadFully(fs3,1024); } // http://www.yoda.arachsys.com/csharp/readbinary.html public static byte[] ReadFully(Stream stream,int initialLength) { // If we've been passed an unhelpful initial length,just // use 32K. if (initialLength < 1) { initialLength = 32768; } byte[] buffer = new byte[initialLength]; int read = 0; int chunk; while ((chunk = stream.Read(buffer,read,buffer.Length - read)) > 0) { read += chunk; // If we've reached the end of our buffer,check to see if there's // any more information if (read == buffer.Length) { int nextByte = stream.ReadByte(); // End of stream? If so,we're done if (nextByte == -1) { return buffer; } // Nope. Resize the buffer,put in the byte we've just // read,and continue byte[] newBuffer = new byte[buffer.Length * 2]; Array.Copy(buffer,newBuffer,buffer.Length); newBuffer[read] = (byte)nextByte; buffer = newBuffer; read++; } } // Buffer is now too big. Shrink it. byte[] ret = new byte[read]; Array.Copy(buffer,ret,read); return ret; }
现在,我更好地指定了我的问题.
使用FileStream和BZip2解压缩文件可以,一切正常.
问题如下:
在字节[]中读取磁盘中的胖大文件(> 500 MB),并将字节发送到Response(asp.net)以进行下载.
使用时
http://www.yoda.arachsys.com/csharp/readbinary.html
public static byte[] ReadFully
我收到错误:OutOfMemoryException …
如果比Stream更好的BufferedStream(FileStream,MemoryStream,…)?
使用BufferedStream,可以读取700 MB的大文件吗? (任何示例代码源使用BufferedStream下载大文件)
我想,这是一个问题:不是“如何读取500mb文件到内存?”但是“如何发送大文件到ASPNET响应流?”
我发现这个代码由Cheeso:
using (var fs = new FileStream(filePath,FileAccess.Read)) { Response.BufferOutput= false; // to prevent buffering byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fs.Read(buffer,buffer.Length)) > 0) { Response.OutputStream.Write(buffer,bytesRead); } }
一个同事说我,使用
Response.TransmitFile(filePath);
现在,另一个问题,更好的TransmitFile或代码由Cheeso?
很多年前,在msdn杂志上出现很好的文章,但是我无法访问http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/,
更新:您可以使用webarchive访问链接:https://web.archive.org/web/20070627063111/http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/
任何建议,意见,示例代码来源?
解决方法
private void TransmitFile(string fullPath,string outFileName) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10000]; // Length of the file: int length; // Total bytes to read: long dataToRead; // Identify the file to download including its path. string filepath = fullPath; // 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.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition","attachment; filename=" + outFileName); Response.AddHeader("Content-Length",iStream.Length.ToString()); // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer,10000); // Write the data to the current output stream. Response.OutputStream.Write(buffer,length); // Flush the data to the output. Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; } } } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { if (iStream != null) { //Close the file. iStream.Close(); } Response.Close(); } }