发送邮件使用的FileStream ASPNET大文件时,500MB OutOfMemoryException异常 [英] OutOfMemoryException when send big file 500MB using FileStream ASPNET
问题描述
我使用的文件流的读取大文件(> 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
Uncom preSS文件中使用的FileStream和bzip2
Uncompress file using FileStream and BZip2
阅读文件uncom pressed并将其发送到asp.net页面下载。
Read file uncompressed and send it to asp.net page for download.
当我从磁盘读取文件,失败!并获得内存不足...
When I read file from disk, Fails !!! and get OutOfMemory...
。我的code是:
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.
Uncom preSS文件中使用的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.
在使用
<一个href=\"http://www.yoda.arachsys.com/csharp/readbinary.html\">http://www.yoda.arachsys.com/csharp/readbinary.html
public static byte[] ReadFully
我得到的错误:OutOfMemoryException异常...
I get the error: OutOfMemoryException...
如果不是流(的FileStream,MemoryStream的,...)效果更佳BufferedStream
If better BufferedStream than Stream (FileStream, MemoryStream, ...) ??
使用BufferedStream,我可以读的700 MB大的文件? (使用BufferedStream下载大文件的任何样本code源代码)
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?"
我发现这个code。通过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);
}
}
这是好code 17高性能的任何改进?
Is it good code ?? any improvements for high performance ??
一个collegue说我,用
A collegue say me, use
Response.TransmitFile(filePath);
现在,另一个问题,更好的TransmitFile或code。通过Cheeso ??
Now, another question, better TransmitFile or code by Cheeso ??
许多年前,在MSDN杂志出现伟大的文章有关,但我不能访问<一个href=\"http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/\">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/,
我觉得页面已被删除:(我很伤心吧(我很高兴,如果有人给我完整的文章+ code)
I think the page was deleted :'( I'm very sad about it (I'm happy if anyone send me the complete article + code)
任何建议,意见,样本code源??
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屋!