By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

Giving the user the option to download a file

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.