php/asp.net/asp下载文件代码

php/asp.net/asp实现文件下载而不是打开代码

PHP版

function DownloadFile($filename, $fullPath ){ 

  // Must be fresh start 
  if( headers_sent() ) 
    die('Headers Sent'); 

  // Required for some browsers 
  if(ini_get('zlib.output_compression')) 
    ini_set('zlib.output_compression', 'Off'); 

  // File Exists? 
  if( file_exists($fullPath) ){ 
    
    // Parse Info / Get Extension 
    $fsize = filesize($fullPath); 
    $path_parts = pathinfo($fullPath); 
    $ext = strtolower($path_parts["extension"]); 
    
    // Determine Content Type 
    switch ($ext) { 
      case "pdf": $ctype="application/pdf"; break; 
      case "exe": $ctype="application/octet-stream"; break; 
      case "zip": $ctype="application/zip"; break; 
      case "doc": $ctype="application/msword"; break; 
      case "xls": $ctype="application/vnd.ms-excel"; break; 
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
      case "gif": $ctype="image/gif"; break; 
      case "png": $ctype="image/png"; break; 
      case "jpeg": 
      case "jpg": $ctype="image/jpg"; break; 
      default: $ctype="application/force-download"; 
    } 

    header("Pragma: public"); // required 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype"); 
    header("Content-Disposition: attachment; filename=\"".$filename."\";" ); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".$fsize); 
    ob_clean(); 
    flush(); 
    readfile( $fullPath ); 

  } else 
    die('File Not Found'); 

} 

ASP.NET版

private void DownloadFile(string fname, bool forceDownload)
    {
        string Filepath = fname;
        string Filename = Path.GetFileName(Filepath);
        string ext = Path.GetExtension(Filepath);
        string type = "";
        // set known types based on file extension  
        if (ext != null)
        {
            switch (ext.ToLower())
            {
                case ".htm":
                case ".html":
                    type = "text/HTML";
                    break;

                case ".txt":
                    type = "text/plain";
                    break;
                case ".doc":
                case ".docx":
                case ".rtf":
                    type = "Application/msword";
                    break;
                case ".wav":
                case  ".Mp3":
 
                    type = "Application/media";
                    break;
 
 
            }
        }
        if (forceDownload)
        {
            Response.AppendHeader("content-disposition",
                "attachment; filename=" + Filename);
        }
        if (type != "")
            Response.ContentType = type;
        Response.WriteFile(Filepath);
        Response.End();
    }
        if (forceDownload)
        {
            Response.AppendHeader("content-disposition",
                "attachment; filename=" + Filename);
        }
        if (type != "")
            Response.ContentType = Filetype;
        Response.WriteFile(Filepath);
        Response.End();
    }

ASP版

Function DownloadFile(fileobj, filename)
	Response.AddHeader "Content-type", "application/x-msdownload;charset=UTF-8"
	response.AddHeader "Content-Disposition","attachment;filename=" & filename 
	Response.BinaryWrite(vbReadFile(fileobj))
    Response.Flush
	Response.End()
End Function
function vbReadFile(filepath,scharset)'//读去文件
	Set fso=CreateObject("Scripting.FileSystemObject")
	filepath = Server.MapPath(filepath)
	if fso.FileExists(filepath) then
		set stm=server.CreateObject("adodb.stream") 
		stm.Type=2 '以本模式读取 
		stm.mode=3 
		stm.charset=scharset
		stm.open 
		stm.loadfromfile filepath
		vbReadFile=stm.readtext 
		stm.Close 
		set stm=nothing
	end if
end function

“php/asp.net/asp下载文件代码”的一个回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注