473,573 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem trying to download a file from ASP.NET page.

I have an ASP.NET page that incorporates the following code on a button
press.

private void DownloadTag_Com mand(object sender, CommandEventArg s e)
{
FileStream fs;
String Filename;

Filename = MapPath(e.Comma ndArgument as string); // Name of file
is passed in cmd arg.

fs = File.Open(Filen ame, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuf fer, 0, (int)fs.Length) ;
fs.Close();

Response.AddHea der("Content-disposition", "attachment ; filename=" +
Path.GetFileNam e(Filename));

Response.Conten tType = "applicatio n/octet-stream";
Response.Binary Write(byteBuffe r);
Response.End();
}

When clicking on my download button in IE, the download will call up the
download file dialog as expected. If I click on Save on the download file
dialog the file downloads without any problem. However, if I click on open,
I get a second download file dialog box with the same file. Not sure why.
If I click on open on this second dialog, the file will download and open as
expected.

Could someone explain how to eliminate this second download file dialog?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 19 '05 #1
2 1651
Ken Varn wrote:
I have an ASP.NET page that incorporates the following code on a
button press.

private void DownloadTag_Com mand(object sender, CommandEventArg s
e) {
FileStream fs;
String Filename;

Filename = MapPath(e.Comma ndArgument as string); // Name of
file is passed in cmd arg.

fs = File.Open(Filen ame, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuf fer, 0, (int)fs.Length) ;
fs.Close();

Response.AddHea der("Content-disposition", "attachment ;
filename=" + Path.GetFileNam e(Filename));

Response.Conten tType = "applicatio n/octet-stream";
Response.Binary Write(byteBuffe r);
Response.End();
}

When clicking on my download button in IE, the download will call up
the download file dialog as expected. If I click on Save on the
download file dialog the file downloads without any problem.
However, if I click on open, I get a second download file dialog box
with the same file. Not sure why. If I click on open on this second
dialog, the file will download and open as expected.

Could someone explain how to eliminate this second download file
dialog?


I don't know why you get that second dialog.

Your code can be somewhat shorter, when you use the Response.WriteF ile
method. You will still need the Content-Disposition, but you don't need
to read in the file yourself.

Hans Kesting

Nov 19 '05 #2
The problem still seems to occur when I use WriteFile as well.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Hans Kesting" <ne***********@ spamgourmet.com > wrote in message
news:eV******** ******@TK2MSFTN GP12.phx.gbl...
Ken Varn wrote:
I have an ASP.NET page that incorporates the following code on a
button press.

private void DownloadTag_Com mand(object sender, CommandEventArg s
e) {
FileStream fs;
String Filename;

Filename = MapPath(e.Comma ndArgument as string); // Name of
file is passed in cmd arg.

fs = File.Open(Filen ame, FileMode.Open);

byte[] byteBuffer = new byte[fs.Length];
fs.Read(byteBuf fer, 0, (int)fs.Length) ;
fs.Close();

Response.AddHea der("Content-disposition", "attachment ;
filename=" + Path.GetFileNam e(Filename));

Response.Conten tType = "applicatio n/octet-stream";
Response.Binary Write(byteBuffe r);
Response.End();
}

When clicking on my download button in IE, the download will call up
the download file dialog as expected. If I click on Save on the
download file dialog the file downloads without any problem.
However, if I click on open, I get a second download file dialog box
with the same file. Not sure why. If I click on open on this second
dialog, the file will download and open as expected.

Could someone explain how to eliminate this second download file
dialog?


I don't know why you get that second dialog.

Your code can be somewhat shorter, when you use the Response.WriteF ile
method. You will still need the Content-Disposition, but you don't need
to read in the file yourself.

Hans Kesting

Nov 19 '05 #3

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

Similar topics

9
2499
by: Theodore A. Jencks | last post by:
Hi All, I have a problem that is two fold. First a little about the enviroment I'm using; I am running Microsoft Windows XP with all security patches applied. I use Mozilla 1.6 for all testing before I test on IE 6 w/ all updates. I am editing my style sheet with StyleMaster 3.5.2 and am hand coding. I use Dreamweaver MX 2004 and am also...
0
1309
by: Richard L Rosenheim | last post by:
I'm looking to display a page and then initiate the downloading of a file. Basically trying to display a page that says the download will begin shortly, and if it doesn't, click here. Like what you see at a number sites. I found code to initiate the download in a previous posting ("Force Download fails when I select "Open" but works when I...
7
5929
by: Sean | last post by:
Hi I've written an HTML page that has a link to an MPG file, to be burned onto a "Demo CD". The MPG file is quite big - 500MB. On my computer, it works fine - clicking the link opens up Windows Media Player, which starts showing the video. But on another computer, the link opens up a new browser page, and shows the video as an embedded...
11
26811
by: Russ | last post by:
My web app writes some binary data to a file at the client site via Response.Write and 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...
7
1969
by: Bala | last post by:
Hi All, I am trying to download the file from network machine (under same domain). I am using below link page code to impersonate. http://www.netomatix.com/ImpersonateUser.aspx I can able to impersonate the user, when i try to download the file, its says "unknown user name and password". I given full permission to domain user. but the...
2
218
by: Flurry | last post by:
Hello I've got a problem and I'm not getting much help trying to resolve it. I've searched google all morning for an answer.... I have the following code: Lab1.text = "Your Serial Number is: " Lab2.text = serialNo & "-" & serialYY
16
3231
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will not print in the username or email amddress of the person doing the download - which I am collecting from a form on the previous page. I can get...
1
1889
by: Blackstar | last post by:
I have a website I am trying to download a file from. This website is strange in the fact that there is no direct link to the URL of the file. The way the site works is as follows: 1) Enter in search criteria 2) Click on Download button to go to download page 3) Click on Download button on Download page 4) Server sends 2 bits of data to...
5
3682
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually for repeated html im using the external js, i mean the TOP, BOTTOM they are repeated in every page, so i include them as functions in the external...
0
7781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8066
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.