文件下載,指定默認名
Response.AddHeader(”content-type”,”application/x-msdownload”);
Response.AddHeader(”Content-Disposition”,”attachment;filename=要下載的文件名.rar”);
刷新頁面
Response.AddHeader “REFRESH”, ”60;URL=newpath/newpage.asp”
這等同於客戶機端元素:
<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);