admin管理员组文章数量:1240605
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?
I tried it using ajax whose code is as below, but I do not get any file downloaded.
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>
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?
I tried it using ajax whose code is as below, but I do not get any file downloaded.
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>
Share
Improve this question
asked Dec 23, 2010 at 12:21
OM The EternityOM The Eternity
16.2k44 gold badges125 silver badges187 bronze badges
3 Answers
Reset to default 7so, How to download a file on clicking the name of file using PHP?
The easiest way should be linking to it.
<a href="download.php?......" target="_blank">DMS.doc</a>
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:
function download(path,val) {
window.location.href = path+"download.php?val="+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 {
本文标签: javascriptHow to download a file on clicking the name of file using PHPStack Overflow
版权声明:本文标题:javascript - How to download a file on clicking the name of file using PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740010207a2220001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论