473,545 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to download a file

I have a .NET server button on a form. When the user
clicks it, I'd like to (behind the scenes) create a pdf
file containing the data that they're currently viewing,
then somehow activate the "Do you want to download or
save this file?" dialog.

The pdf file I'm already creating. That's no big deal.
But now, how do I make this newly-created file available
for the user to download? Can I activate that dialog
somehow? I'd like to do this in one single click of
this .NET button (System.Web.UI. WebControls.But ton).

Thanks.

Nov 17 '05 #1
2 1190
Easiest way is to create the file inside of the published root directory
somewhere and then after you've created it do something like:

Response.Redire ct("~/createdfiles/mydocument.pdf" );

This should achieve what you're looking for....
"Yuriy" <yl******@hotma il.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
I have a .NET server button on a form. When the user
clicks it, I'd like to (behind the scenes) create a pdf
file containing the data that they're currently viewing,
then somehow activate the "Do you want to download or
save this file?" dialog.

The pdf file I'm already creating. That's no big deal.
But now, how do I make this newly-created file available
for the user to download? Can I activate that dialog
somehow? I'd like to do this in one single click of
this .NET button (System.Web.UI. WebControls.But ton).

Thanks.

Nov 17 '05 #2
Visual C# .NET Code Sample:
System.IO.Strea m iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = "DownloadFileNa me";

// Identify the file name.
string filename = System.IO.Path. GetFileName(fil epath);

try
{
// Open the file.
iStream = new System.IO.FileS tream(filepath, System.IO.FileM ode.Open,
System.IO.FileA ccess.Read,Syst em.IO.FileShare .Read);
// Total bytes to read:
dataToRead = iStream.Length;

Response.Conten tType = "applicatio n/octet-stream";
Response.AddHea der("Content-Disposition", "attachment ; filename=" +
filename);

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsCli entConnected)
{
// Read the data in buffer.
length = iStream.Read(bu ffer, 0, 10000);

// Write the data to the current output stream.
Response.Output Stream.Write(bu ffer, 0, length);

// Flush the data to the HTML output.
Response.Flush( );

buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write( "Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close() ;
}
}
Replace DownloadFileNam e with the name of a file .


"Yuriy" <yl******@hotma il.com> дÈëÓʼþ
news:0f******** *************** *****@phx.gbl.. .
I have a .NET server button on a form. When the user
clicks it, I'd like to (behind the scenes) create a pdf
file containing the data that they're currently viewing,
then somehow activate the "Do you want to download or
save this file?" dialog.

The pdf file I'm already creating. That's no big deal.
But now, how do I make this newly-created file available
for the user to download? Can I activate that dialog
somehow? I'd like to do this in one single click of
this .NET button (System.Web.UI. WebControls.But ton).

Thanks.

Nov 17 '05 #3

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

Similar topics

5
6091
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily downloads. It uses fpassthru() and some headers() to send a file to the requesting user. The get.php file that I wrote (the file download module if you...
0
1819
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog window. Well, thanks to Steve Orr, I have that working. However, I still have a few issues. First, the download dialog does not have the name of the...
0
1732
by: Rhys666 | last post by:
Basically I have a link that opens my download page and the querystring identifies the type of 'template' Excel spreadsheet has asked to download. The download page reads the querystring, identifies the template required then uses Response.AppendHeader to amend the response to create the file download dialogue. On my local machine this works...
4
2319
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 dialog boxes that says something like "Do you want to download FILEX.txt?" I want the user to be able to download the file rather than have the file...
1
3559
by: a.r.austin | last post by:
Hello, I am trying to download a few files one after another from a remote server. Problem is that I don't know how to, or if I am able at all, set a time out for download. I don't want to time out whole script, just a part if file won't download in 2mins then skip to the next one. Previously, I had a Javascript implementation with AJAX,...
3
2125
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. The problem is that when you run the program it gets to the: Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
16
3226
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...
0
2152
by: kain razial | last post by:
ANYOTHER NEEDS ALSO CONTACT ME.I WILL HELP YOU TO GET THAT TEKLA XSTEEL V 12 DOWNLOAD LINK. http://www.sendspace.com/file/zem32r
0
4000
by: kain razial | last post by:
ANYOTHER NEEDS ALSO CONTACT ME.I WILL HELP YOU TO GET THAT TEKLA XSTEEL V 12 DOWNLOAD LINK. http://www.sendspace.com/file/zem32r
1
47376
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or...
0
7484
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
7928
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
7775
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
5997
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
4963
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
3470
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...
1
1902
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
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
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.