php、asp以“流格式”下载服务器端文件
作者:admin 日期:2012-04-10
解决WORD,EXCEL,TXT,图片等文件类型直接在IE中被打开的问题
ASP/Visual Basic代码
- <%
- Function downFileFromServer(showFileName, downFilePath)
- response.Clear
- response.Buffer = true
- Set ads = Server.CreateObject("ADODB.Stream")
- ads.Type = 1
- ads.Mode = 3
- ads.Open
- ads.LoadFromFile downFilePath
- response.AddHeader("Content-Encoding", "None")
- response.AddHeader("Cache-Control", "Private")
- response.AddHeader("Content-Length", ads.Size)
- response.AddHeader("Content-Disposition", "attachment; filename=" & showFileName)
- response.ContentType = "application/octet-stream"
- response.BinaryWrite(ads.Read(ads.Size))
- ads.Close
- Set ads = nothing
- response.Flush
- response.End
- End Function
- Call downFileFromServer("abc.test", Server.MapPath("test.asp"))
- %>
PHP代码
- <?php
- function downFileFromServer($showFileName, $downFilePath)
- {
- if(file_exists($downFilePath))
- {
- if(is_readable($downFilePath))
- {
- if(Trim($showFileName) == '')
- {
- $showFileName = 'undefined';
- }
- ob_start();
- ob_clean();
- $file_size = filesize($downFilePath);
- header('Content-Encoding:none');
- header('Cache-Control:private');
- header('Content-Length:' . $file_size);
- header('Content-Disposition:attachment; filename=' . $showFileName);
- header('Content-Type:application/octet-stream');
- readfile($downFilePath);
- ob_flush();
- }
- }
- }
- //Sample
- downFileFromServer('a.abc', 'test.php');
- ?>
[本日志由 admin 于 2012-04-13 01:28 PM 更新]
上一篇: php遍历文件夹和文件下一篇: php javascript cookie中文乱码解决办法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1665
发表评论
广告位