<p>ASP利用FSO及Stream方法实现IE直接下载</p> <p>在IE进行文档链接时,如果遇到OLE支持的文档,IE会自动调用相应程序打开它,有时候这种功能并不是我们所需的,虽然我们可以提醒用户用鼠标右键--&gt;&quot;目标另存为....&quot;命令来下载文档,但这样毕竟不太友好,本文描述了利用FSO及Stream方法实现IE直接下载文档.<br /> &lt;%@ language=vbscript codepage=65001%&gt;</p> <p>&lt;% <br /> 'Filename must be input<br /> if Request(&quot;Filename&quot;)=&quot;&quot; then<br /> response.write &quot;&lt;h1&gt;Error:&lt;/h1&gt;Filename is empty!&lt;p&gt;&quot;<br /> else<br /> call downloadFile(replace(replace(Request(&quot;Filename&quot;),&quot;\&quot;,&quot;&quot;),&quot;/&quot;,&quot;&quot;)) <br /> <br /> Function downloadFile(strFile) <br /> ' make sure you are on the latest MDAC version for this to work <br /> ' get full path of specified file <br /> strFilename = server.MapPath(strFile) <br /> <br /> ' clear the buffer <br /> Response.Buffer = True <br /> Response.Clear <br /> <br /> ' create stream <br /> Set s = Server.CreateObject(&quot;ADODB.Stream&quot;) <br /> s.Open <br /> <br /> ' Set as binary <br /> s.Type = 1 <br /> <br /> ' load in the file <br /> on error resume next <br /> <br /> ' check the file exists<br /> Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> if not fso.FileExists(strFilename) then <br /> Response.Write(&quot;&lt;h1&gt;Error:&lt;/h1&gt;&quot;&amp;strFilename&amp;&quot; does not exists!&lt;p&gt;&quot;) <br /> Response.End <br /> end if<br /> <br /> ' get length of file <br /> Set f = fso.GetFile(strFilename) <br /> intFilelength = f.size <br /> <br /> s.LoadFromFile(strFilename) <br /> if err then <br /> Response.Write(&quot;&lt;h1&gt;Error: &lt;/h1&gt;Unknown Error!&lt;p&gt;&quot;) <br /> Response.End <br /> end if</p> <p>' send the headers to the users Browse<br /> Response.AddHeader &quot;Content-Disposition&quot;,&quot;attachment; filename=&quot;&amp;f.name <br /> Response.AddHeader &quot;Content-Length&quot;,intFilelength <br /> Response.CharSet = &quot;UTF-8&quot; <br /> Response.ContentType = &quot;application/octet-stream&quot;</p> <p>' output the file to the browser <br /> Response.BinaryWrite s.Read <br /> Response.Flush</p> <p>' tidy up <br /> s.Close <br /> Set s = Nothing</p> <p>End Function <br /> end if<br /> %&gt;</p>
T:0.004186s,M:238.22 KB
返回顶部 留言