javascript 如何使用PHP单击文件名下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4518702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:59:08  来源:igfitidea点击:

How to download a file on clicking the name of file using PHP?

phpjavascriptajax

提问by OM The Eternity

I Have a list of information in which there is a field where name of file is hyperlinked. I want the user to download that particular file when he clicks on it.

我有一个信息列表,其中有一个字段,其中文件名是超链接的。我希望用户在点击它时下载该特定文件。

so, How to download a file on clicking the name of file using PHP?

那么,如何使用PHP单击文件名来下载文件?

I tried it using ajax whose code is as below, but I do not get any file downloaded.

我使用 ajax 尝试了它,其代码如下,但我没有下载任何文件。

download.php

下载.php

$filename = $_GET['val'];
         // Fetch the file info.
 $filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;

    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }

javascript function

javascript函数

<script>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function download(path,val) {
var req = Inint_AJAX();
req.open("GET", path+"download.php?val="+val); //make connection
//req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {

      }
 }
};

}
</script>

link to download

下载链接

<a href="javascript:download('http://localhost/project/images/','DMS.doc');">DMS.doc</a>

回答by Pekka

so, How to download a file on clicking the name of file using PHP?

那么,如何使用PHP单击文件名来下载文件?

The easiest way should be linking to it.

最简单的方法应该是链接到它。

<a href="download.php?......" target="_blank">DMS.doc</a>

回答by German Rumm

You don't need AJAX for that, you cannot even do what you want using AJAX. If you want to obfuscate URL to a file, then replace your download function with this:

你不需要 AJAX,你甚至不能用 AJAX 做你想做的事。如果要将 URL 混淆为文件,请将下载功能替换为:

function download(path,val) {
    window.location.href = path+"download.php?val="+val;
};

回答by ????? ??????

     // Fetch the file info.


$filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;



if(file_exists($filePath)) {
    $fileName = basename($filePath);
    $fileSize = filesize($filePath);

    // Output headers.
    header("Cache-Control: private");
    header("Content-Type: application/stream");
    header("Content-Length: ".$fileSize);
    header("Content-Disposition: attachment; filename=".$fileName);

    // Output file.
    readfile ($filePath);                   
    exit();
}
else {