string UserBrowser=Request.Browser.Browser;//瀏覽器名稱
string BrowserVersion=Request.Browser.Version; //瀏覽器版本
以下摘自MSDN
HttpBrowserCapabilities bc = Request.Browser;
Response.Write( " <p> Browser Capabilities: </p> ");
Response.Write( "Type = " + bc.Type + " <br> ");
Response.Write( "Name = " + bc.Browser + " <br> ");
Response.Write( "Version = " + bc.Version + " <br> ");
Response.Write( "Major Version = " + bc.MajorVersion + " <br> ");
Response.Write( "Minor Version = " + bc.MinorVersion + " <br> ");
Response.Write( "Platform = " + bc.Platform + " <br> ");
Response.Write( "Is Beta = " + bc.Beta + " <br> ");
Response.Write( "Is Crawler = " + bc.Crawler + " <br> ");
Response.Write( "Is AOL = " + bc.AOL + " <br> ");
Response.Write( "Is Win16 = " + bc.Win16 + " <br> ");
Response.Write( "Is Win32 = " + bc.Win32 + " <br> ");
Response.Write( "Supports Frames = " + bc.Frames + " <br> ");
Response.Write( "Supports Tables = " + bc.Tables + " <br> ");
Response.Write( "Supports Cookies = " + bc.Cookies + " <br> ");
Response.Write( "Supports VB Script = " + bc.VBScript + " <br> ");
Response.Write( "Supports JavaScript = " + bc.JavaScript + " <br> ");
Response.Write( "Supports Java Applets = " + bc.JavaApplets + " <br> ");
Response.Write( "Supports ActiveX Controls = " + bc.ActiveXControls + " <br> ");
Response.Write( "CDF = " + bc.CDF + " <br> ");
如何在下載檔案名稱中使用UTF-8編碼
著作權聲明:可以任意轉載,但轉載時必須標明原作者charlee、原始連結http://tech.idv2.com/2009/03/05/use-utf8-in-download-filename/以及本聲明。
通過把Content-Type設定為application/octet-stream, 可以把動態產生的內容當作檔案來下載,相信這個大家都會。 那麼用Content-Disposition設定下載的檔案名稱, 這個也有不少人知道吧。 基本上,下載程式都是這麼寫的:
<?php
$filename = "document.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
這樣用瀏覽器開啟之後,就可以下載document.doc。
但是,如果$filename是UTF-8編碼的,有些瀏覽器就無法正常處理了。 比如把上面那個程式稍稍改一下:
<?php
$filename = "中文 檔案名稱.txt";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
print "Hello!";
?>
把程式儲存成UTF-8編碼再訪問,IE6下載的檔案名稱就會亂碼。 FF3下下載的檔案名稱就只有“中文”兩個字。Opera 9下一切正常。
輸出的header實際上是這樣子:
Content-Disposition: attachment; filename=中文 檔案名稱.txt
其實按照RFC2231的定義, 多語言編碼的Content-Disposition應該這麼定義:
Content-Disposition: attachment; filename*="utf8''%E4%B8%AD%E6%96%87%20%E6%96%87%E4%BB%B6%E5%90%8D.txt"
即:
- filename後面的等號之前要加 *
- filename的值用單引號分成三段,分別是字元集(utf8)、語言(空)和urlencode過的檔案名稱。
- 最好加上雙引號,否則檔案名稱中空格後面的部分在Firefox中顯示不出來
- 注意urlencode的結果與php的urlencode函數結果不太相同,php的urlencode會把空格替換成+,而這裡需要替換成%20
經過實驗,發現幾種主流瀏覽器的支援情況如下:
IE6 |
attachment; filename="<URL編碼之後的UTF-8檔案名稱>" |
FF3 |
attachment; filename="UTF-8檔案名稱" |
attachment; filename*="utf8''<URL編碼之後的UTF-8檔案名稱>" |
O9 |
attachment; filename="UTF-8檔案名稱" |
Safari3(Win) |
貌似不支援?上述方法都不行 |
這樣看來,程式必須得這樣寫才能支援所有主流瀏覽器:
<?php
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文 檔案名稱.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
header('Content-Type: application/octet-stream');
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
print 'ABC';
?>