C#实现大文件分块发送到客户端

I have an interesting little problem here that only occurs when clients
using IE 5 on Mac OS 9 use my ASP.net app.


I have one page in the application that allows users to download zip files
of selected content. The user clicks on a link like <a
href="stream_file.aspx">Download Zip File</a>

The file stream_file.aspx basically implements the file streaming code
referenced in MS Knowledge Base Article 812406. The actual code is at the
end of this message.

This stream_file.aspx page works perfectly in every browser/OS except IE5
on Mac OS 9. When IE 5 on the Mac connects, it appears as though the
entire file is streamed into memory on the webserver before the Mac
attempts to download it, then once the Mac has finished the download the
server does not release the memory used by the download. Thus I end up
running out of memory on the server after about a dozen downloads.

An example might make my problem a bit clearer:
1. A user selects a group of files to download as a single zip and then
clicks the download link.
2. The server creates the Zip file (I've confirmed that the memory used by
the zip creation process is being released).
3. The server then streams the zip file from a temp directory to the
client.
4. The user is immediately prompted for the location and filename to save
the zip file as. However, during this time the memory utilization on the
server increases by 5 times the size of the zip file (i.e. a 10 MB zip
file consumes 50 MB of memory during this transfer).
5. Until the entire file is streamed to memory on the server, IE on the
Mac is displaying a status of "Waiting for data". Once the file is
streamed to memory, IE on the Mac begins the actual download.
6. Once the download is completed, the memory on the server does not get
released (I've let the server sit for an hour with no activity to see if
the GC would do the cleanup, but it did not).

It should be noted that in IE 4-6 and Netscape 4.7-7.0 on the PC, Netscape
4.7 on Mac OS 9, and IE 5 on Mac OS 10 all work perfectly with the memory
consumption during the download taking no more than 1 MB of RAM which is
released after the download completes.

Does anyone have any suggestions on how I can force the memory to be
released after the download? Or any information on what is actually
causing the memory utilization in this one case?

Thank you,


Here is the source code for the stream file function:


private void StreamFile(string SourceFilepath, string
OutputFilename)
{
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;

try
{
// Open the file.
iStream = new System.IO.FileStream(SourceFilepath,
System.IO.FileMode.Open);

// Total bytes to read:
dataToRead = iStream.Length;

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;
filename=" + OutputFilename);
Response.AddHeader("Content-Length", dataToRead.ToString
());

// Read the bytes.
while (dataToRead > 0)
{
// NOTE: Cannot do a Response.IsClientConnected check
// here (as was in the original code) due to
// not being able to detect a connection from a mac
// download manager.

// 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 HTML output.
Response.Flush();

buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
}
catch (Exception ex)
{
// Trap the error, if any.
System.Diagnostics.EventLog AppEventLog = new
System.Diagnostics.EventLog("Application");
AppEventLog.WriteEntry("Glamorise
Website:/members/stream_file.aspx:StreamFile()::The following error
occurred: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
AppEventLog.Dispose();
}
finally
{
if (iStream != null)
{
// Close the file.
iStream.Close();
}
}
}



 

你可能感兴趣的:(C#实现大文件分块发送到客户端)