我有一个信息列表,其中有一个字段名称是超链接的字段.我希望用户在点击它时下载该特定文件.
download.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
<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>
解决方法:
so, How to download a file on clicking the name of file using PHP?
<a href="download.PHP?......" target="_blank">DMS.doc</a>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。