Problem with Response after downloading a file

H

hoenes1

I've got an aspx-Page "SendFile.aspx" which is called by a Link on
"ShowListOfFiles.aspx" and sends the file in the OnLoad Eventhandler.
The filename to download is stored in a Session variable. I'm sending
the file to the client in 4 KB chunks. Sending the file works fine,
but when I click a link on the calling aspx-Page
("ShowListOfFiles.aspx") after the download completed, the browser
window shows fragments of the recently transmitted file and a plain
text HTTP header. Looks like the Response buffer isn't cleared
correctly.

Here's the relevant code in "SendFile.aspx":

private void Page_Load(object sender, System.EventArgs e)
{
// prerequisites, e. g. open FileStream, ...
while (offset < size)
{
if (size-offset < bufSize)
bufSize = size-offset;
fs.Read(buf, 0, (int)bufSize);
Response.BinaryWrite(buf);
Response.Flush();
offset += bufSize;
}
fs.Close();
// I've tried Response.Close(), Response.End(),
Response.Clear(), ...
}

Anyone knows this issue and a solution or workaround?

Thanks in advance.
 
S

spalding

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 sam
except;

void Page_Load()
{
Response.Buffer = true;
Response.ContentType = strContentType;
Response.AddHeader("Content-Disposition",
"attachment; filename="
+ strFileName
);
//Response.BinaryWrite code goes here.



-
spaldin
 
H

hoenes1

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top