Hi,
thanks for your response. I actually did tell the browser what type
the file is. It was in the "// prerequisites" section. I found the
solution myself. But since many postings in this group contain the
same error in the sending code, I'll post the working version of
sending files in chunks (since this is the only effective way to send
large files to a client). The error was that I didn't resize the
byte[] buffer size to match exactly the number of bytes remaining to
be sent. If you don't resize the buffer, the Response.BinaryWrite()
function always sends a multiple of <bufSize> bytes which doesn't
correspond to the real file size. Here's the code (but please read
further at the end of the code, one issue is still remaining):
private void Page_Load(object sender, System.EventArgs e)
{
string file = Convert.ToString(Session["filePath"]);
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read,
FileShare.Read);
long size = fs.Length;
long bufSize = 65536;
long offset = 0;
byte[] buf = new byte[bufSize];
Response.ContentType="application/octet-stream";
Response.AppendHeader("content-disposition","attachment;
filename="+CHelper.extractName(file));
Response.AppendHeader("content-length", size.ToString());
while (offset < size)
{
if (size-offset < bufSize)
{
bufSize = size-offset;
buf = new byte[bufSize]; // here was the mistake
}
fs.Read(buf, 0, (int)bufSize);
if (Response.IsClientConnected)
{
Response.BinaryWrite(buf);
Response.Flush();
}
else
break;
offset += bufSize;
}
fs.Close();
Response.Close();
}
The issue mentioned above is the following:
When the client cancels a download while it is running, the server
should be informed about this. The common solution is to check the
Response.IsClientConnected property (like I do in my code). The
problem is that IsClientConnected IS DEFINITELY NOT set to false on
the client cancelling the download. Someone knows if this is a .NET
bug or even knows a workaround?
Thanks again in advance.
spalding <sp*************@mail.codecomments.com> wrote in message news:<sp*************@mail.codecomments.com>...
Hi
I think you need to tell the browser what type of file it is;
I've got the same functionality and my code looks exactly the same
except;
void Page_Load()
{
Response.Buffer = true;
Response.ContentType = strContentType;
Response.AddHeader("Content-Disposition",
"attachment; filename="
+ strFileName
);
//Response.BinaryWrite code goes here.
}