ASP實現文件直接下載

          在IE進行文檔鏈接時,如果遇到OLE支持的文檔,IE會自動調用相應程序打開它,有時候這種功能并不是我們所需的,雖然我們可以提醒用戶用鼠標右鍵-->"目標另存為...."命令來下載文檔,但這樣畢竟不太友好,本文描述了利用FSO及Stream方法實現IE直接下載文檔.

          <%@?language=vbscript?codepage=65001%>

          <%?
          'Filename?must?be?input
          if?Request("Filename")=""?then
          ?response.write?"<h1>Error:</h1>Filename?is?empty!<p>"
          else
          call??downloadFile(replace(replace(Request("Filename"),"\",""),"/",""))????
          ?
          Function??downloadFile(strFile)????
          '??make??sure??you??are??on??the??latest??MDAC??version??for??this??to??work????
          '??get??full??path??of??specified??file????
          strFilename??=??server.MapPath(strFile)????
          ?
          '??clear??the??buffer????
          Response.Buffer??=??True????
          Response.Clear????
          ?
          '??create??stream????
          Set??s??=??Server.CreateObject("ADODB.Stream")????
          s.Open????
          ?
          '??Set??as??binary????
          s.Type??=??1????
          ?
          '??load??in??the??file????
          on??error??resume??next????
          ?
          '??check??the??file??exists
          Set??fso??=??Server.CreateObject("Scripting.FileSystemObject")????
          if??not??fso.FileExists(strFilename)??then????
          Response.Write("<h1>Error:</h1>"&strFilename&"?does?not?exists!<p>")????
          Response.End????
          end??if
          ?
          '??get??length??of??file????
          Set??f??=??fso.GetFile(strFilename)????
          intFilelength??=??f.size????
          ?
          s.LoadFromFile(strFilename)????
          if??err??then????
          Response.Write("<h1>Error:?</h1>Unknown?Error!<p>")????
          Response.End?
          end??if??

          '??send??the??headers??to??the??users??Browse
          Response.AddHeader??"Content-Disposition","attachment;??filename="&f.name????
          Response.AddHeader??"Content-Length",intFilelength????
          Response.CharSet??=??"UTF-8"????
          Response.ContentType??=??"application/octet-stream"????

          '??output??the??file??to??the??browser????
          Response.BinaryWrite??s.Read????
          Response.Flush????

          '??tidy??up????
          s.Close????
          Set??s??=??Nothing????

          End??Function????
          end?if
          %>??????

          posted on 2006-06-01 17:03 【Xine】中文站 閱讀(206) 評論(0)  編輯  收藏 所屬分類: ASP Program


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(8)

          隨筆分類(40)

          隨筆檔案(40)

          文章分類(33)

          文章檔案(34)

          相冊

          BLOG 聯盟

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 万州区| 桐城市| 华坪县| 多伦县| 龙胜| 阳高县| 江阴市| 文水县| 普兰店市| 巴林右旗| 阿瓦提县| 资中县| 天长市| 石家庄市| 山东| 潢川县| 山西省| 武隆县| 万盛区| 唐山市| 翁源县| 化隆| 高密市| 韩城市| 林西县| 齐河县| 隆林| 巴林右旗| 申扎县| 澳门| 南雄市| 景谷| 泗洪县| 江门市| 丹巴县| 乐业县| 长垣县| 嘉祥县| 连云港市| 寿阳县| 从江县|