国产探花免费观看_亚洲丰满少妇自慰呻吟_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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 勐海县| 西藏| 白朗县| 濮阳县| 新化县| 大理市| 色达县| 龙山县| 都兰县| 平武县| 宝兴县| 汾西县| 柳河县| 阜南县| 庆元县| 博爱县| 涞水县| 新和县| 巴塘县| 兖州市| 昆山市| 额济纳旗| 罗田县| 肇源县| 盐边县| 水城县| 从化市| 新闻| 思南县| 元江| 咸丰县| 乌兰浩特市| 米林县| 上栗县| 湘西| 江陵县| 寻乌县| 白玉县| 从江县| 宜春市| 双流县|