PALMisLIFE 討論區

搜索
鹹魚爸魅力四射舞蹈教室
查看: 35202|回復: 2
打印 上一主題 下一主題

[求助] [HTML] 下載檔案的語法!

[複製鏈接]

293

主題

0

好友

2300

積分

Johnny Joker

  • TA的每日心情
    無聊
    2012-7-5 13:24
  • 簽到天數: 1 天

    連續簽到: 1 天

    [LV.1]初來乍到

    文章
    1910
    跳轉到指定樓層
    1#
    發表於 2004-8-17 10:20 |只看該作者 |正序瀏覽
    要在網頁上讓User下載.txt檔案

    <a href="abc.txt">文字</a>
    使用這種方式會直接開啟文件,而不會出現下載視窗

    解決方法一:壓縮成ZIP檔
    解決方法二:按右鍵另存新檔

    不過以上二種方法都不可行,因為User對電腦操作不熟(就是很多人說的電腦白X)

    能不能"點一下左鍵就下載"?!

    (JavaScript、ASP、HTML 語法皆可)

    [ Last edited by johnnyk on 2004-8-17 at 10:21 ]
    分享淘帖0 分享分享0 收藏收藏0 頂0 踩0

    3

    主題

    0

    好友

    115

    積分

  • TA的每日心情
    開心
    2012-6-21 22:52
  • 簽到天數: 9 天

    連續簽到: 1 天

    [LV.3]偶爾看看II

    文章
    113
    3#
    發表於 2004-8-17 21:44 |只看該作者

    Re: [求助] [HTML] 下載檔案的語法!

    這段原碼是從藍色小舖來的哦,應該改一下(檔案類性的地方)或不用改就可以了,沒有測試ASP的平台懶得試了 …

    download.asp 的內容:

    1. <%
    2. Response.Buffer = True
    3. Dim strFilePath, strFileSize, strFileName
    4. Const adTypeBinary = 1
    5. strFilePath = Request.QueryString("File")
    6. strFileSize = Request.QueryString("Size")
    7. strFileName = Request.QueryString("Name")
    8. Response.Clear
    9. Set objStream = Server.CreateObject("ADODB.Stream")
    10. objStream.Open
    11. objStream.Type = adTypeBinary
    12. objStream.LoadFromFile server.mappath(strFilePath)
    13. strFileType = lcase(Right(strFileName, 4))
    14. Select Case strFileType
    15. Case ".asf"
    16. ContentType = "video/x-ms-asf"
    17. Case ".avi"
    18. ContentType = "video/avi"
    19. Case ".doc"
    20. ContentType = "application/msword"
    21. Case ".zip"
    22. ContentType = "application/zip"
    23. Case ".xls"
    24. ContentType = "application/vnd.ms-excel"
    25. Case ".gif"
    26. ContentType = "image/gif"
    27. Case ".jpg", "jpeg"
    28. ContentType = "image/jpeg"
    29. Case ".wav"
    30. ContentType = "audio/wav"
    31. Case ".mp3"
    32. ContentType = "audio/mpeg3"
    33. Case ".mpg", "mpeg"
    34. ContentType = "video/mpeg"
    35. Case ".rtf"
    36. ContentType = "application/rtf"
    37. Case ".htm", "html"
    38. ContentType = "text/html"
    39. Case ".asp"
    40. ContentType = "text/asp"
    41. Case Else
    42. ContentType = "application/octet-stream"
    43. End Select
    44. Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
    45. Response.AddHeader "Content-Length", strFileSize
    46. Response.Charset = "UTF-8"
    47. Response.ContentType = ContentType
    48. Response.BinaryWrite objStream.Read
    49. Response.Flush
    50. objStream.Close
    51. Set objStream = Nothing
    52. %>
    複製代碼


    用「download.asp?File=檔名」的連結就可以直接下載了 ~
    ← 剛踏入PDA殿堂的新新手 :)
    回復

    使用道具 舉報

    104

    主題

    4

    好友

    1429

    積分

    羊毛大亨

    該用戶從未簽到

    文章
    1533
    2#
    發表於 2004-8-17 11:14 |只看該作者

    Re: [求助] [HTML] 下載檔案的語法!

    這要從  http 的 header 下手...
    不是 html 的 header 喔!

    這邊是 RFC
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

    通常要配合主機端語言來做控制(ASP, PHP, JSP, Perl, CGI 等), 無法單獨的用
    1. <a href="download.txt">下載</a>
    複製代碼

    這樣來解決.

    我提供我參考別人修改來的ㄧ段 PHP 程式碼:

    1. function echoFileHeader($mName, $mSize, $mContent = 'application/octet-stream')
    2. {
    3.         GLOBAL $HTTP_SERVER_VARS;

    4.         if(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE')){
    5.                 // IE cannot download from sessions without a cache
    6.                 header('Cache-Control: public');
    7.         } else {
    8.                 header('Cache-Control: private');
    9.         }
    10.         header("Content-type: $mContent");
    11.         header("Content-length:" . (string)($mSize) );
    12.         //header("Content-Disposition:inline; filename=\"" . $mName . "\"");
    13.         if (strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE')) {
    14.                 header("Content-Disposition:attachment; filename=\"" . $mName . "%20\"");
    15.         } else {
    16.                 header("Content-Disposition:attachment; filename=\"" . $mName . "\"");
    17.         }

    18.         return TRUE;
    19. }
    複製代碼

    這邊的 header() 是對 HTTP 送出 HEADER, 並不是 HTML 裡面的 header, 請注意.
    對 IE 要對檔名另作處理, 好像是因為 5.0 or 6.0 的一個 bug(記不太得了)
    有問題再繼續討論~
    回復

    使用道具 舉報

    您需要登錄後才可以回帖 登錄 | 免費註冊

    與站長聯繫| PALMisLIFE 掌上生活      下載:更快、更棒、更好玩

    GMT+8, 2024-9-20 15:05 , Processed in 0.096710 second(s), 31 queries , Gzip On.

    Powered by Discuz!

    © 2001-2012 Comsenz Inc. style by eisdl

    回頂部