473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with Response after downloading a file

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.
Nov 18 '05 #1
2 1897

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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 18 '05 #2
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.
}

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: moondaddy | last post by:
Using vb.net I need to download image files to the client browser where they can save to disk. Below is some sample code I'm using. when I run this the File Download window in the browser says: ...
0
by: Amol Guttal via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Amol Guttal Hi, This is the code which used to download a file from webserver. If I point a file in my localhost for download...
3
by: Jocelyn Duhaylungsod | last post by:
I have been getting the following server errors sporadically while downloading excel, csv or xml file. Internet Explorer cannot download <file name> from <IP address> "The server returned an...
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
5
by: Tim_Mac | last post by:
hi, i read that by adding the following code to by aspx pages, it would not store temporary internet files: Response.Cache.SetCacheability(HttpCacheability.NoCache); it didn't actually work...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
4
by: PiotrKolodziej | last post by:
hi I have a thread that downloades a file. Problem is : Not all files are beeing downloaded. I observed that only the small files are beeing downloaded correctly. I also cant download two files...
1
by: alex23 | last post by:
Hey everyone, I'm trying to install setuptools on a work PC behind an NTLM firewall. I've tried to use APS as recommended but am still unable to have anything other than IE talk through...
6
by: cyusman | last post by:
Hi, We have just moved our application to a new webfarm server environment which utilizing hardware load balancing, SSL off-loading and HTTP compression off-loading.My application is running on...
1
by: shahidrasul | last post by:
i want to download a file which user select from gridview, downloading is completing without problem but after download i want to refresh my page because i do some changes in db . but when...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.