asp.net – OutOfMemoryException当发送大文件500MB使用FileStream ASPNET

前端之家收集整理的这篇文章主要介绍了asp.net – OutOfMemoryException当发送大文件500MB使用FileStream ASPNET前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Filestream读取大文件(> 500 MB),并获得OutOfMemoryException.

我使用Asp.net,.net 3.5,win2003,iis 6.0

我想在我的应用程序中:

从Oracle读取DATA

使用FileStream和BZip2解压缩文件

读取文件未压缩并发送到asp.net页面进行下载.

当我从磁盘读取文件,失败!!!并获得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/

任何建议,意见,示例代码来源?

解决方法

我创建了下载页面,允许用户在几个月前下载高达4gb(可能更多).这是我的工作片段:
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();
        }
    }

猜你在找的asp.Net相关文章