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】中文站 閱讀(212) 評論(0)  編輯  收藏 所屬分類: ASP Program


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


          網站導航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(8)

          隨筆分類(40)

          隨筆檔案(40)

          文章分類(33)

          文章檔案(34)

          相冊

          BLOG 聯盟

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 陆良县| 清涧县| 正阳县| 阿拉善左旗| 钟山县| 丹阳市| 汕尾市| 恭城| 瓦房店市| 淮南市| 句容市| 大连市| 甘孜| 丹巴县| 罗定市| 南通市| 玉屏| 汉源县| 公主岭市| 准格尔旗| 嵩明县| 汤原县| 长宁县| 鹤山市| 迁安市| 巴东县| 五台县| 孝昌县| 阿瓦提县| 嘉兴市| 曲水县| 平山县| 宿迁市| 社旗县| 禄劝| 玉屏| 乌鲁木齐市| 繁昌县| 阿荣旗| 新晃| 广南县|