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.