472,353 Members | 1,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
4 2244
response.addHeader "content-disposition", "attachment;
filename=filename.ext"

should do the work...
"Nathan Sokalski" <nj********@hotmail.com> schrieb im Newsbeitrag
news:ej**************@TK2MSFTNGP09.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********@hotmail.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_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDownload.Click

Dim mystreamwriter As System.IO.TextWriter

mystreamwriter = Response.Output

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

mystreamwriter.Write(Date.Now.ToLongDateString() & " " &
Date.Now.ToLongTimeString())

mystreamwriter.Close()

Response.AddHeader("content-disposition", "attachment;
filename=download.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********@hotmail.com
http://www.nathansokalski.com/

"Thomas Egginger" <T.Egginger@__gmx__.net> wrote in message
news:eE*************@TK2MSFTNGP09.phx.gbl...
response.addHeader "content-disposition", "attachment;
filename=filename.ext"

should do the work...
"Nathan Sokalski" <nj********@hotmail.com> schrieb im Newsbeitrag
news:ej**************@TK2MSFTNGP09.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********@hotmail.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********@hotmail.com> wrote in message
news:OQ**************@tk2msftngp13.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_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDownload.Click

Dim mystreamwriter As System.IO.TextWriter

mystreamwriter = Response.Output

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

mystreamwriter.Write(Date.Now.ToLongDateString() & " " &
Date.Now.ToLongTimeString())

mystreamwriter.Close()

Response.AddHeader("content-disposition", "attachment;
filename=download.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********@hotmail.com
http://www.nathansokalski.com/

"Thomas Egginger" <T.Egginger@__gmx__.net> wrote in message
news:eE*************@TK2MSFTNGP09.phx.gbl...
response.addHeader "content-disposition", "attachment;
filename=filename.ext"

should do the work...
"Nathan Sokalski" <nj********@hotmail.com> schrieb im Newsbeitrag
news:ej**************@TK2MSFTNGP09.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********@hotmail.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(object sender, System.EventArgs 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.FileInfo file = new System.IO.FileInfo(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.AddHeader("Content-Disposition", "attachment; filename=" +
file.Name);

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

// can show the download progress

Response.AddHeader("Content-Length", file.Length.ToString());

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

// client and must be downloaded

Response.ContentType = "application/octet-stream";

// send the file stream to the client

Response.WriteFile(file.FullName);

// 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********@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.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********@hotmail.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
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...
5
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...
2
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...
1
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...
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...
2
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...
1
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...
6
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.