473,394 Members | 1,840 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,394 software developers and data experts.

Download file, browser is filled with funny characters.

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
2 2157
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: aa | last post by:
Is it OK to include an ANSI file into a UTF-8 file?
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: Norton | last post by:
Hi all, I would like to know if there is any method in asp or asp.net to direct the browser to download a file into the Temporary Internet Files directory so that i can make use of vb script to...
1
by: Daniel Cardoso | last post by:
I don't know what else to try - In my asp.net app, when the file name has Windows-1252 characters (like ã and ç), these characters appear, each one, as two strange characters in the file name label...
4
by: Nathan Sokalski | last post by:
I want to give visitors to my site the option of downloading a generated ..txt file by clicking a button. I know how to generate text files, but how do I cause the browser to pop up one of those...
12
by: devospice | last post by:
Hi, I'm trying to create a download counter for individual files on a web site and I'm not sure how to do this. Right now I'm using Webalizer to just read the log files and see how many times...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.