473,848 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Giving the user the option to download a file

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 displayed in the
browser, because the file will include some characters such as tabs and
commas. The purpose of the text file is a tab delimited text file, and I do
not want the browser modifying any of the characters. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
4 2343
response.addHea der "content-disposition", "attachment ;
filename=filena me.ext"

should do the work...
"Nathan Sokalski" <nj********@hot mail.com> schrieb im Newsbeitrag
news:ej******** ******@TK2MSFTN GP09.phx.gbl...
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 displayed in the
browser, because the file will include some characters such as tabs and
commas. The purpose of the text file is a tab delimited text file, and I do
not want the browser modifying any of the characters. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

Nov 19 '05 #2
That seems to solve the problem of bringing up the dialog box, however, it
does not seem to be letting me generate the file the way I want. The code I
am using is:

Private Sub btnDownload_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnDownload.Cli ck

Dim mystreamwriter As System.IO.TextW riter

mystreamwriter = Response.Output

mystreamwriter. WriteLine("This is a test download text file")

mystreamwriter. Write(Date.Now. ToLongDateStrin g() & " " &
Date.Now.ToLong TimeString())

mystreamwriter. Close()

Response.AddHea der("content-disposition", "attachment ;
filename=downlo ad.txt")

End Sub
This code brings up the dialog box, but saves only the code that you would
see if you looked at the browser's View->Source. If I place the code at the
very beginning of the Page_Load procedure, then the generated text is placed
at the beginning of the file, but everything else is still included with it.
I realize I could make a separate file that has nothing other than the code
to generate the file and bring up the download dialog, but I would like to
be able to have the user click a button, have the file generated, and then
have the download dialog come up without creating a separate .aspx file.
What do I need to do differently? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Thomas Egginger" <T.Egginger@__g mx__.net> wrote in message
news:eE******** *****@TK2MSFTNG P09.phx.gbl...
response.addHea der "content-disposition", "attachment ;
filename=filena me.ext"

should do the work...
"Nathan Sokalski" <nj********@hot mail.com> schrieb im Newsbeitrag
news:ej******** ******@TK2MSFTN GP09.phx.gbl...
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 displayed in the
browser, because the file will include some characters such as tabs and
commas. The purpose of the text file is a tab delimited text file, and I
do not want the browser modifying any of the characters. Any ideas?
Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/


Nov 19 '05 #3
You could place everything inside of one panel control. This way when the
user clicks the download button you can set the panel visible and enable
properties to false. Your best bet though may be to place the download
generation in another page, which is usually the typical way to do it as it
does avoid this annoying issue.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:OQ******** ******@tk2msftn gp13.phx.gbl...
That seems to solve the problem of bringing up the dialog box, however, it
does not seem to be letting me generate the file the way I want. The code
I am using is:

Private Sub btnDownload_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnDownload.Cli ck

Dim mystreamwriter As System.IO.TextW riter

mystreamwriter = Response.Output

mystreamwriter. WriteLine("This is a test download text file")

mystreamwriter. Write(Date.Now. ToLongDateStrin g() & " " &
Date.Now.ToLong TimeString())

mystreamwriter. Close()

Response.AddHea der("content-disposition", "attachment ;
filename=downlo ad.txt")

End Sub
This code brings up the dialog box, but saves only the code that you would
see if you looked at the browser's View->Source. If I place the code at
the very beginning of the Page_Load procedure, then the generated text is
placed at the beginning of the file, but everything else is still included
with it. I realize I could make a separate file that has nothing other
than the code to generate the file and bring up the download dialog, but I
would like to be able to have the user click a button, have the file
generated, and then have the download dialog come up without creating a
separate .aspx file. What do I need to do differently? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"Thomas Egginger" <T.Egginger@__g mx__.net> wrote in message
news:eE******** *****@TK2MSFTNG P09.phx.gbl...
response.addHea der "content-disposition", "attachment ;
filename=filena me.ext"

should do the work...
"Nathan Sokalski" <nj********@hot mail.com> schrieb im Newsbeitrag
news:ej******** ******@TK2MSFTN GP09.phx.gbl...
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 displayed in the
browser, because the file will include some characters such as tabs and
commas. The purpose of the text file is a tab delimited text file, and I
do not want the browser modifying any of the characters. Any ideas?
Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/



Nov 19 '05 #4
Nathan,

You have to send the appropriate headers to popup that dialog box.
Generally you can put such code in the Page_load, here is a sample of how to
do it.

private void Page_Load(objec t sender, System.EventArg s e)

{

// retrieve the path of the file to download, and create

// a FileInfo object to read its properties

string path = Server.MapPath( Request.Params["File"]);

System.IO.FileI nfo file = new System.IO.FileI nfo(path);

// clear the current output content from the buffer

Response.Clear( );

// add the header that specifies the default filename for the
Download/SaveAs dialog

Response.AddHea der("Content-Disposition", "attachment ; filename=" +
file.Name);

// add the header that specifies the file size, so that the browser

// can show the download progress

Response.AddHea der("Content-Length", file.Length.ToS tring());

// specify that the response is a stream that cannot be read by the

// client and must be downloaded

Response.Conten tType = "applicatio n/octet-stream";

// send the file stream to the client

Response.WriteF ile(file.FullNa me);

// stop the execution of this page

Response.End();

}
- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------
"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:ej******** ******@TK2MSFTN GP09.phx.gbl...
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 displayed in the
browser, because the file will include some characters such as tabs and
commas. The purpose of the text file is a tab delimited text file, and I do
not want the browser modifying any of the characters. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

Nov 19 '05 #5

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

Similar topics

7
3666
by: Navin | last post by:
hi, i am using the follwoing code to write a recordset to a csv file 'fcreateFile -file system object Do While Not oRs.EOF fcreateFile.Write(oRs.GetString(,1000,",",vbcrlf,"")) 'Write Login Info loop
5
9205
by: dotyet | last post by:
I have been given the daunting task of sql query tuning. I am looking for ways to get started with that. I am on DB2 UDB 8.2 (8.1 with Fixpak 8) on Windows. One point which I could think about starting from is the db2 design advisor. However, whenever I try to execute the db2advis utility I get the following error: C:\db_tuning>db2advis -d DB_TEST1 -user db2admin/db2admin -i query_1.sql Using user id as default schema name. Use -n...
2
330
by: R.A. | last post by:
Hi In the web site I work on I give the users an option to download files from the web server. They click a button and I use the Response.Redirect("filename.exe"). The problem is that the user migth see the downloaded file url. Is there a way to hide the downloaded file url (it shows in the ie taskbar). Server.Transfer doesn't work for this. Thanks
1
3293
by: connoisseur.infotech | last post by:
i have schedule a .exe of VB6.0 on a remote computer and when that exe run out i got this message in scheduler. operating system is window server 2003 where i have scheduled it. This program actually download a file from ftp location and then fill the database with ODBC driver to MYSQL and at last close that file and delete that file. I don't get any error when i run that program without scheduler. Please
4
1242
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 displayed in the browser, because the file will include some characters such as tabs and commas....
2
1868
by: torbjorn.maro | last post by:
I'm the developer of an enterprise level web application that among other things handles documents. The documents are stored in a database. There is now simple functionality to check out and download the document. This is done my a normal response to the browser, streaming the document. The user then gets the normal option to 'open', 'save' or 'cancel' til downloaded file. Normal usage would be to save the file to a known location, open...
1
1967
by: Radhey Krishna | last post by:
A very interesting problem for a simple requirement and not easily available solution: How would I create a button which will allow the user to specify where the file to be downloaded will be saved to?? The actual problem is that I have multiple drop down buttons (for multiple files) with each of the drop down button containing an option to save to a local folder. Once the user specifies the local path for each of these files and...
6
2387
by: vstud70 | last post by:
Dear friends, I have a website that sells software. In my download page I have a "download" button that once the user clicks it open the windows wizard to /download/save/cancel the software. So far so good. The download itself is down using the response.redirect (url of the file to download), e.g. response.redirect("assets/setup.exe") My question is how do i also redirect the user to a 'thank you' page once they clicked the...
0
9735
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10996
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10347
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9497
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7062
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4541
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
2
4133
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3171
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.