Asp.net hyperlink to download the TEXT file, instead of directly opening it in IE, asp. nettext

Source: Internet
Author: User

Asp.net hyperlink to download the TEXT file, instead of directly opening it in IE, asp. nettext

Problem description: a text file is generated in the background and provided to the user for download through a hyperlink. Click the hyperlink to download the Excel file. However, the text file will be opened directly, rather than the download window will pop up.

Solution: Change HyperLink to LinkButton. In the Click event, use a file stream to write text files.

Key code:

 1 protected void Button1_Click(object sender, EventArgs e) 2 { 3     string sFileName = System.IO.Path.GetRandomFileName(); 4     string sGenName = "Friendly.txt"; 5  6     //YOu could omit these lines here as you may 7     //not want to save the textfile to the server 8     //I have just left them here to demonstrate that you could create the text file  9     using (System.IO.StreamWriter SW = new System.IO.StreamWriter(10            Server.MapPath("TextFiles/" + sFileName + ".txt")))11     {12         SW.WriteLine(txtText.Text);13         SW.Close();14     }15 16     System.IO.FileStream fs = null;17     fs = System.IO.File.Open(Server.MapPath("TextFiles/" + 18              sFileName + ".txt"), System.IO.FileMode.Open);19     byte[] btFile = new byte[fs.Length];20     fs.Read(btFile, 0, Convert.ToInt32(fs.Length));21     fs.Close();22     Response.AddHeader("Content-disposition", "attachment; filename=" + 23                        sGenName);24     Response.ContentType = "application/octet-stream";25     Response.BinaryWrite(btFile);26     Response.End();27 }

 

Reference: http://www.codeproject.com/useritems/textfile.asp


How can I open a file in the IE hyperlink instead of opening it directly?

The A tag is used to link files. If you are linking an HTML file, open it in IE and the content will be automatically explained and displayed in IE. If other files, such as the rarfile, are downloaded
 
How to make the hyperlink of tag a in html always download files, instead of opening files in IE

The A tag is used to link files.
If you are linking an HTML file, open it in IE and the content will be automatically explained and displayed in IE.
If another file, such as the rarfile, is displayed, the file download prompt is displayed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.