By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

Download file, browser is filled with funny characters.

P: n/a
My web app writes some binary data to a file at the client site via
Response.BinaryWrite. This action is accomplished
in response to a button click, with C# code behind as follows:

private void SubmitButton_Click (object sender, System.EventArgs e)
{
// Set up the response to write the print file to the client
Response.Clear ();
Response.Buffer=false;

Response.AppendHeader ("Content-Disposition",
"filename=WebPrint.prn");
Response.AppendHeader ("Content-Length", filename.length.ToString
());
Response.ContentType = "application/octet-stream";

Response.BinaryWrite (buf); // Write binary data
Response.Flush();

Response.End ();

}
This works fine for first time. User sees a File Save As dialog box
and download a file to the local drive. But my problem is that when
user clicks 'Download' button again, the browser is filled with junk
funny characters. If I close the browser after the download and open
it again then click download button, it works always. Most of time, I
get this behavior when I click download button for the second time,
sometimes third click.

Please help.

Oct 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
See inline
My web app writes some binary data to a file at the client site via
Response.BinaryWrite. This action is accomplished
in response to a button click, with C# code behind as follows:

private void SubmitButton_Click (object sender, System.EventArgs e)
{
// Set up the response to write the print file to the client
Response.Clear ();
Response.Buffer=false;

Response.AppendHeader ("Content-Disposition",
"filename=WebPrint.prn");
Change the line above to:
Response.AppendHeader ("Content-Disposition",
"attachment;filename=WebPrint.prn");
Response.AppendHeader ("Content-Length", filename.length.ToString
());
Response.ContentType = "application/octet-stream";

Response.BinaryWrite (buf); // Write binary data
Response.Flush();

Response.End ();

}

This works fine for first time. User sees a File Save As dialog box
and download a file to the local drive. But my problem is that when
user clicks 'Download' button again, the browser is filled with junk
funny characters. If I close the browser after the download and open
it again then click download button, it works always. Most of time, I
get this behavior when I click download button for the second time,
sometimes third click.

Please help.

Oct 5 '06 #2

P: n/a
Hans,
Thanks for your reply,
I did changed AppendHeader line and tried.
But I got the same result. In this time, I was able to click download
button 4 time before my browser is filled with funny characters. I am
trying many different ways to download files.
Context.Response.Clear();
Context.Response.ContentType = "application/octet-stream";
Context.Response.AppendHeader("Content-Disposition", "attachment;
filename=" + pstrFileName);
Context.Response.AppendHeader("Content-Length",
objStream.Length.ToString());
Context.Response.Buffer=false;

byte[] buffer = new byte[m_bufferSize];
long byteCount;
try
{

while ((byteCount = objStream.Read(buffer, 0, buffer.Length)) 0)
{
if(Context.Response.IsClientConnected)
{
Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
Context.Response.Flush();
}
else
{
OnDownloadCancelled();
return;
}
}
OnDownloadCompleted();
}
catch(Exception ex)
{
throw ex;
}
finally
{
objStream.Close();
Context.Response.End();
}
Hans Kesting wrote:
See inline
My web app writes some binary data to a file at the client site via
Response.BinaryWrite. This action is accomplished
in response to a button click, with C# code behind as follows:

private void SubmitButton_Click (object sender, System.EventArgs e)
{
// Set up the response to write the print file to the client
Response.Clear ();
Response.Buffer=false;

Response.AppendHeader ("Content-Disposition",
"filename=WebPrint.prn");

Change the line above to:
Response.AppendHeader ("Content-Disposition",
"attachment;filename=WebPrint.prn");
Response.AppendHeader ("Content-Length", filename.length.ToString
());
Response.ContentType = "application/octet-stream";

Response.BinaryWrite (buf); // Write binary data
Response.Flush();

Response.End ();

}

This works fine for first time. User sees a File Save As dialog box
and download a file to the local drive. But my problem is that when
user clicks 'Download' button again, the browser is filled with junk
funny characters. If I close the browser after the download and open
it again then click download button, it works always. Most of time, I
get this behavior when I click download button for the second time,
sometimes third click.

Please help.
Oct 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.