- 註冊時間
 - 2002-8-8
 - 線上時間
 - 3361 小時
 - 閱讀權限
 - 200
 - 積分
 - 1429
 - 主題
 - 104
 - 精華
 - 1
 - 文章
 - 1533
  
 
 
 
該用戶從未簽到 - 文章
 - 1533
  
 | 
Re: [求助] [HTML] 下載檔案的語法!
| 
 
 這要從  http 的 header 下手... 
不是 html 的 header 喔! 
 
這邊是 RFC 
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 
 
通常要配合主機端語言來做控制(ASP, PHP, JSP, Perl, CGI 等), 無法單獨的用 
- <a href="download.txt">下載</a>
 
  複製代碼 
這樣來解決. 
 
我提供我參考別人修改來的ㄧ段 PHP 程式碼: 
 
- function echoFileHeader($mName, $mSize, $mContent = 'application/octet-stream')
 
 - {
 
 -         GLOBAL $HTTP_SERVER_VARS;
 
  
-         if(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE')){
 
 -                 // IE cannot download from sessions without a cache
 
 -                 header('Cache-Control: public');
 
 -         } else {
 
 -                 header('Cache-Control: private');
 
 -         }
 
 -         header("Content-type: $mContent");
 
 -         header("Content-length:" . (string)($mSize) );
 
 -         //header("Content-Disposition:inline; filename=\"" . $mName . "\"");
 
 -         if (strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE')) {
 
 -                 header("Content-Disposition:attachment; filename=\"" . $mName . "%20\"");
 
 -         } else {
 
 -                 header("Content-Disposition:attachment; filename=\"" . $mName . "\"");
 
 -         }
 
  
-         return TRUE;
 
 - } 
 
 
  複製代碼 
這邊的 header() 是對 HTTP 送出 HEADER, 並不是 HTML 裡面的 header, 請注意. 
對 IE 要對檔名另作處理, 好像是因為 5.0 or 6.0 的一個 bug(記不太得了) 
有問題再繼續討論~ |   
 
  
 |