Board logo

标题: php,asp解决WORD,EXCEL,TXT,图片等文件类型直接在IE中被打开的问题 [打印本页]

作者: fangzhen    时间: 2009-4-29 08:52     标题: php,asp解决WORD,EXCEL,TXT,图片等文件类型直接在IE中被打开的问题

一、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');
?>        

二、ASP

<%
    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开发笔记 (http://phpvi.com/) Powered by Discuz! 6.1.0