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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 射洪县| 桑日县| 崇明县| 堆龙德庆县| 日照市| 延川县| 固安县| 阿荣旗| 长治市| 界首市| 景宁| 呈贡县| 中阳县| 凉城县| 克山县| 海林市| 枣阳市| 法库县| 上蔡县| 宁强县| 濮阳市| 富民县| 汾西县| 峨山| 万源市| 尼木县| 麻阳| 海原县| 登封市| 德令哈市| 得荣县| 乾安县| 濮阳县| 龙井市| 东明县| 江孜县| 兴和县| 铅山县| 措勤县| 辽阳县| 普安县|