使用 FileStream ASPNET 发送 500MB 大文件时出现 OutOfMemoryException [英] OutOfMemoryException when send big file 500MB using FileStream ASPNET

查看:22
本文介绍了使用 FileStream ASPNET 发送 500MB 大文件时出现 OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Filestream 读取大文件 (> 500 MB),但出现 OutOfMemoryException.

I'm using Filestream for read big file (> 500 MB) and I get the OutOfMemoryException.

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

I use Asp.net , .net 3.5, win2003, iis 6.0

我希望在我的应用中使用这个:

I want this in my app:

从 Oracle 读取数据

Read DATA from Oracle

使用 FileStream 和 BZip2 解压缩文件

Uncompress file using FileStream and BZip2

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

Read file uncompressed and send it to asp.net page for download.

当我从磁盘读取文件时,失败!!!并获得 OutOfMemory...

When I read file from disk, Fails !!! and get 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; 
  } 

现在,我更好地说明了我的问题.

Now, I specify my issue better.

使用 FileStream 和 BZip2 解压文件就可以了,一切正常.

Uncompress file using FileStream and BZip2 is OK, all is right.

问题如下:

以字节[]读取磁盘中的胖大文件(> 500 MB)并将字节发送到响应(asp.net)以进行下载.

Read fat big file in disk (> 500 MB) in byte[] and send bytes to Response (asp.net) for download it.

使用时

http://www.yoda.arachsys.com/csharp/readbinary.html

public static byte[] ReadFully

我收到错误:OutOfMemoryException...

I get the error: OutOfMemoryException...

如果 BufferedStream 比 Stream (FileStream, MemoryStream, ...) 更好 ??

If better BufferedStream than Stream (FileStream, MemoryStream, ...) ??

使用 BufferedStream ,我可以读取 700 MB 的大文件吗??(任何使用 BufferedStream 下载大文件的示例代码源)

Using BufferedStream , Can I read big file of 700 MB ?? (any sample code source using BufferedStream for download big file)

我认为,问题是:不是如何将 500mb 的文件读入内存?", 但是如何将大文件发送到 ASPNET 响应流?"

I think, this is the question: Not "how to read a 500mb file into memory?" , But "how to send a large file to the ASPNET Response stream?"

我找到了 Cheeso 的代码:

I found this code by Cheeso:

using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))  
{  
   Response.BufferOutput= false;   // to prevent buffering 
   byte[] buffer = new byte[1024]; 
   int bytesRead = 0; 
   while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)  
   { 
       Response.OutputStream.Write(buffer, 0, bytesRead); 
   } 
}

这是好的代码吗??对高性能有什么改进吗??

Is it good code ?? any improvements for high performance ??

同事说我,用

Response.TransmitFile(filePath);

现在,另一个问题,更好的 TransmitFile 或 Cheeso 代码 ??

Now, another question, better TransmitFile or code by Cheeso ??

许多年前,在 msdn 杂志上出现了关于它的很棒的文章,但我无法访问 http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/,

Many years ago, in msdn magazine appears great article about it but I cannot access 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/

Update: You can access using webarchive in the link: https://web.archive.org/web/20070627063111/http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/

任何建议、评论、示例代码源??

Any suggestions, comments, sample code source??

推荐答案

几个月前我创建了下载页面,允许用户下载高达 4gb(可能更多).这是我的工作片段:

I've created download page which allows user to download up to 4gb (may be more) few months ago. Here is my working snippet:

  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, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, 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();
        }
    }

这篇关于使用 FileStream ASPNET 发送 500MB 大文件时出现 OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆