天天看點

Response.AppendHeader使用大全

檔案下載下傳,指定預設名

Response.AddHeader(”content-type”,”application/x-msdownload”);

Response.AddHeader(”Content-Disposition”,”attachment;filename=要下載下傳的檔案名.rar”);

重新整理頁面

Response.AddHeader “REFRESH”, ”60;URL=newpath/newpage.asp”

這等同于客戶機端<META>元素:

<META HTTP-EQUIV=”REFRESH”, “60;URL=newpath/newpage.asp”

頁面轉向

Response.Status = “302 Object Moved”

Response.Addheader “Location”, “newpath/newpage.asp”

這等同于使用Response.Redirect方法:

Response.Redirect “newpath/newpage.asp”

強制浏覽器顯示一個使用者名/密碼對話框

Response.Status= “401 Unauthorized”

Response.Addheader “WWW-Authenticate”, “BASIC”

強制浏覽器顯示一個使用者名/密碼對話框,然後使用BASIC驗證把它們發送回伺服器(将在本書後續部分看到驗證方法)。

如何讓網頁不緩沖

Response.Expires = 0

Response.ExpiresAbsolute = Now() - 1

Response.Addheader “pragma”,”no-cache”

Response.Addheader “cache-control”,”private”

Response.CacheControl = “no-cache

應用執行個體:檔案下載下傳

做下載下傳中文顯示亂碼怎麼辦

在網站上檔案下載下傳都是直接點選檔案聯接就行了,這種方法有幾個弊端:

1. 有些檔案不會下載下傳會直接調用相應的程式打開該檔案

2。不能隐藏實際檔案位址。

3。不能夠從資料庫中動态讀取檔案名進行改名下載下傳

下面是asp.net,c#代碼:

string fileName;//檔案在資料庫中的名稱

string dir ;//檔案在伺服器的實體路徑(如c:\aa\ddd\wj0000222.zdo)

long size ;//檔案的大小

Response.AddHeader(”content-type”, “application/x-msdownload;”);

Response.AddHeader(”Content-Disposition”,”attachment;filename=”+fileName[自己定義的]);

Response.AddHeader(”content-length”, size.ToString());

Response.WriteFile(dir,0,size);

這種方法可以實作以上的目的,但是當檔案名(fileName)為中文時在ie下載下傳端顯示的是亂碼,有誰知道怎麼解決。

我來做個總結吧 

         其實樓上的方法是可行的,但有局限性 

         關鍵在于UrlEncode這個東東,在下面不同情況下的結果是不一樣的 

         1。web.config         裡responseEncoding=”gb2312″   

         2。web.config         裡responseEncoding=”utf-8″ 

         使用Server.UrlEncode的話必須responseEncoding=”utf-8″才會正确 

         是以不要用Server.UrlEncode,換HttpUtility.UrlEncode 

         string         s=HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(”中文.txt”)); 

         Response.AppendHeader(”Content-Disposition”,         “attachment;         filename=”         +         s);