Giving the user the option to download a file

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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.
 
T

Thomas Egginger

response.addHeader "content-disposition", "attachment;
filename=filename.ext"

should do the work...
 
N

Nathan Sokalski

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.
 
M

Mark Fitzpatrick

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
 
S

Sahil Malik [MVP]

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.malik/archive/2005/05/13/63199.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top