Response對象具有如下四種功能:
1、輸出數據
2、跳轉頁面
3、輸出文件
4、傳遞參數
第一個功能:
很簡單,就是純碎的輸出,例如:Response.Write("對你有幫助麼");
第二個功能:
寫一個語句就明白了,如:Response.Redirect("http://www.baidu.com");
第三個功能:
給定代碼,自己琢磨琢磨,Reaponse對象的使用。
代碼如下:
protected void Button6_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/test.doc");//文件的路徑(虛擬路徑)
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "utf-8";//設置輸出的編碼
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,爲"文件下載/另存爲"對話框指定默認文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個不能被客戶端讀取的流,必須被下載
Response.ContentType = "application/msword";
// 把文件流發送到客戶端
Response.WriteFile(file.FullName);
Response.End();
}
注:用一個按鈕實現文件輸出。
第四個功能:
前面已經說過了,很清楚,請點擊