Download file, browser is filled with funny characters.

M

msxkim

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.
 
H

Hans Kesting

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");
 
M

msxkim

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();
}
 

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,836
Messages
2,569,750
Members
45,545
Latest member
rapter____0

Latest Threads

Top