国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

asp.net 超鏈接 下載TEXT文件,而不是直接在IE中打開

2019-11-17 01:49:54
字體:
來源:轉載
供稿:網友

asp.net 超鏈接 下載TEXT文件,而不是直接在IE中打開

問題描述:后臺生成了文本文件,用超鏈接提供給用戶下載。點擊超鏈接,下載Excel文件沒問題,但文本文件會直接被打開,而不是彈出下載窗口。

解決方法:把HyperLink改為LinkButton,在Click事件中,用文件流寫文本文件。

關鍵代碼:

 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 }

參考文檔:http://www.codeproject.com/useritems/textfile.asp


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仁化县| 红原县| 扶沟县| 微博| 商洛市| 柏乡县| 喀什市| 灌云县| 白朗县| 新龙县| 长丰县| 烟台市| 板桥市| 上虞市| 舟山市| 余江县| 旺苍县| 廉江市| 荆门市| 永修县| 海盐县| 大港区| 轮台县| 赤壁市| 清新县| 清丰县| 盱眙县| 崇仁县| 城固县| 靖宇县| 安陆市| 安福县| 大悟县| 郯城县| 璧山县| 胶南市| 乐清市| 青海省| 闵行区| 兴文县| 南皮县|