<%
'filepath 文件地址
'file_Rname 显示给客户的文件名function downloadFile(filepath,file_Rname)
'on error resume nextresponse.buffer=trueSet fso = Server.CreateObject("Scripting.FileSystemObject") 
Set f = fso.GetFile(filepath) 
Set S=CreateObject("Adodb.Stream") 
S.Mode=3 
S.Type=1 
S.Open 
S.LoadFromFile(filepath)'开始下载
if Err.Number>0 then 
  Response.Status="404"
else
  
  Response.AddHeader "Content-Disposition","attachment; filename=" & file_Rname  Response.CharSet = "UTF-8"
  'application/download_only  使浏览器无法辨别文件类型,防止在浏览器内直接打开
  Response.ContentType="application/download_only"
  Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)
  if Range="" then
   Response.BinaryWrite(S.Read)
  else
   S.position=Clng(Split(Range,"-")(0))
   '支持断点续传
   Response.BinaryWrite(S.Read)
  End ifResponse.Endend function
%>实现只下载的 asp代码

解决方案 »

  1.   

    try:
    <?
    @session_start();
    error_reporting('0');
    if(!isset($File))$File = "temp.txt";$Path = "./upfiles/";
    $filestail=strrchr($File,".");
    $namestr = str_replace($filestail,"",$File);
    $strs = split("-",$namestr);
    $Path .= "D".$strs[2]."/";
    header("Content-type: application/x-gzip");
    header("Content-Disposition: attachment; filename=$File");
    header("Content-Description: PHP Generated Data");
    readfile($Path.$File);?>
      

  2.   

    function download()
    {
    $upload_dir= "../".$_GET['path'];
    $upload_file  = $_GET['filename'];
    $fp = fopen($upload_dir.$upload_file,"r"); 
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: ".filesize($upload_dir.$upload_file));
    Header("Content-Disposition: attachment; filename=" . $upload_file);
    echo fread($fp,filesize($upload_dir . $upload_file));
    fclose($fp);
    }
      

  3.   

    不能用改名的方式,更不能右键另存...
    我对php接触没有几天。。 sandyuk(冰の沙隆) 和bonniewater(陪你去看海) 两位大侠能否详细解释该怎么做,该放到哪里,我的下载链接该怎么写阿?
      

  4.   

    而且我写header时会包错
    Warning: Cannot add header information - headers already sent by (output started at c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\grid.php:7) in c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\grid.php on line 8
      

  5.   

    <a href=download.php?filename=a.txt&path=c:/>下载</a>
    你把这个函数写到一个文件download.php中
    download.php
    <?php
    function download()
    {
    $upload_dir= "../".$_GET['path'];
    $upload_file  = $_GET['filename'];
    $fp = fopen($upload_dir.$upload_file,"r"); 
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: ".filesize($upload_dir.$upload_file));
    Header("Content-Disposition: attachment; filename=" . $upload_file);
    echo fread($fp,filesize($upload_dir . $upload_file));
    fclose($fp);
    }
    download();
    ?>
      

  6.   

    http://www.51base.com/article/search.asp?page=1&action=search&search=PHP+%CF%C2%D4%D8&typeid=0
      

  7.   

    十分感谢!!请问bonniewater(陪你去看海)
    我写了之后会报错:这是什么原因,是我的设置问题嘛?
    错误信息如下:
    Warning: Cannot add header information - headers already sent by (output started at c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php:5) in c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php on line 13Warning: Cannot add header information - headers already sent by (output started at c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php:5) in c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php on line 14Warning: Cannot add header information - headers already sent by (output started at c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php:5) in c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php on line 15Warning: Cannot add header information - headers already sent by (output started at c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php:5) in c:\Program Files\Apache Group\Apache\htdocs\hcycom\host\download.php on line 16