計算機學習積累

          ----轉載有理,轉載是想研究,想研究才會看,看了才會有感想,轉載后我有時會寫一些自己的感受
          數據加載中……

          在ASP.NET中下載Text文件,而不是在瀏覽器中打開它(轉)

          介紹

          讓用戶從我們的網站上下載各種類型的文件是一個比較常用的功能,這篇文章就是告訴您如何創建一個.txt文件并讓用戶下載。

          使用代碼

          雖然在示例里,我先創建了一個text文件,但是你不一定也要這么做,因為這個文件可能在你的網站里已經存在了。如果是這樣的話,你只需要使用FileStream去讀取它就可以了。

          首先,我們將這個text文件讀取到一個byte數組中,然后使用Response對象將文件寫到客戶端就可以了。

          Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
          Response.ContentType = "application/octet-stream";
          Response.BinaryWrite(btFile);
          Response.End();


          這段代碼是完成這個功能的主要代碼。第一句在輸出中添加了一個Header,告訴瀏覽器我們發送給它的是一個附件類型的文件。然后我們設置輸出的ContentType是"application/octet-stream",即告訴瀏覽器要下載這個文件,而不是在瀏覽器中顯示它。

          下面是一個MIME類型的列表。
          ".asf" = "video/x-ms-asf"
          ".avi" = "video/avi"
          ".doc" = "application/msword"
          ".zip" = "application/zip"
          ".xls" = "application/vnd.ms-excel"
          ".gif" = "image/gif"
          ".jpg"= "image/jpeg"
          ".wav" = "audio/wav"
          ".mp3" = "audio/mpeg3"
          ".mpg" "mpeg" = "video/mpeg"
          ".rtf" = "application/rtf"
          ".htm", "html" = "text/html"
          ".asp" = "text/asp"

          '所有其它的文件
          = "application/octet-stream"

          下面是一個完整的如何下載文本文件的示例代碼
          C#
          protectedvoid Button1_Click(object sender, EventArgs e)
          {
          string sFileName = System.IO.Path.GetRandomFileName();
          string sGenName = "Friendly.txt";
          //YOu could omit these lines here as you may not want to save the textfile to the server
          //I have just left them here to demonstrate that you could create the text file
          using (System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("TextFiles/" + sFileName + ".txt")))
          {
          SW.WriteLine(txtText.Text);
          SW.Close();
          }

          System.IO.FileStream fs = null;
          fs = System.IO.File.Open(Server.MapPath("TextFiles/" + sFileName + ".txt"), System.IO.FileMode.Open);
          byte[] btFile = newbyte[fs.Length];
          fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
          fs.Close();
          Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
          Response.ContentType = "application/octet-stream";
          Response.BinaryWrite(btFile);
          Response.End();
          }

          VB.NET
          Dim strFileName AsString = System.IO.Path.GetRandomFileName()
          Dim strFriendlyName AsString = "Friendly.txt"

          Using sw AsNew System.IO.StreamWriter(Server.MapPath("TextFiles/" + strFileName + ".txt"))
          sw.WriteLine(txtText.Text)
          sw.Close()
          EndUsing

          Dim fs As System.IO.FileStream = Nothing


          fs = System.IO.File.Open(Server.MapPath("TextFiles/" + strFileName + ".txt"), System.IO.FileMode.Open)
          Dim btFile(fs.Length) AsByte
          fs.Read(btFile, 0, fs.Length)
          fs.Close()
          With Response
          .AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
          .ContentType = "application/octet-stream"
          .BinaryWrite(btFile)
          .End()
          EndWith
          小結
          使用這個方法,你可以實現在Windows系統下下載所有的文件類型。但是在Macintosh系統下會有些問題。

          posted on 2008-02-18 14:32 freebird 閱讀(698) 評論(0)  編輯  收藏 所屬分類: dotnet

          主站蜘蛛池模板: 海安县| 开阳县| 公安县| 民权县| 景泰县| 通山县| 清新县| 威信县| 康马县| 曲水县| 丘北县| 武冈市| 涟水县| 屯门区| 英德市| 肇州县| 微博| 略阳县| 仙游县| 伊宁市| 建瓯市| 黔南| 郁南县| 郓城县| 秀山| 伊春市| 莲花县| 丹凤县| 左权县| 长岭县| 无为县| 微山县| 泌阳县| 剑川县| 郁南县| 西峡县| 闸北区| 射阳县| 保定市| 隆德县| 靖西县|