前台调用:js调用:
function downloadfile(id,name,price,curcount_pricelimit){
Date.prototype.Format = function(fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var time = new Date().Format("yyyy-MM-dd-hh-mm-ss");
var fileName = name + "_" + time + ".txt";
//alert(fileName); window.open("downloadfile.php?fileName=" + fileName+"&id=" + id + "&name=" + name + "&price=" + price+"&curcount_pricelimit="+curcount_pricelimit); //进度条和下载文件,创建标志文件 //$.ajax("download.php?id=" + id + "&name=" + name + "&price=" + price + "&fileName=" + fileName); //生成文件 }
php 调用:
echo "<a style='color:blue' onclick=\"downloadfile(".$values['id'].",'".$values['name']."',".$values['lowest_price'].",".$values['curcount_pricelimit'].")\" href='javascript:viod(0)' target=_blank>下载</a>";
下载文件:download.php
<?php //下载数据
header('Content-Type:text/html;charset=utf-8');
//$filePath="/minbao/data/amazon/data/";
$filePath = "d:/www/amazon/data/";//此处给出你下载的文件在服务器的什么地方 $crawlId=$_GET['id'];
$price=$_GET['price'];//最低价格
$name=urldecode($_GET['name']);
$name=iconv("utf-8","gb2312",$name);//解决乱码
$time=date("Y-m-d-H-i-s"); //导出sql语句:Select pid,price Into OutFile 'F:/QQPCmgr/Desktop/Data_OutFile.txt' fields terminated by ',' From `t_product`
//导出文件命名格式Crawl_ID_txt;
$fileName=$_GET['fileName']; //删除文件
if(file_exists($filePath.$fileName)){
unlink($filePath.$fileName);
} include_once('./mysql.php');
$conndb=new ConnDB(); $str="crawl_id='".$crawlId."' and price>='".$price."' ";
//查询配置好的关键词
$filterSqlStr='';
$sql="select filterword from t_crawl_configuration where id=".$crawlId;
$request=$conndb->queryarr($sql);
if($request){
if(strlen($request[0][0])>0)
$filterSqlStr=" and name NOT REGEXP '".$request[0][0]."' ";
}else{
echo "error";
exit;
} $sql="select pid,price,name Into OutFile '".$filePath.$fileName."' fields terminated by ',' lines terminated by '\r\n' From `t_product` where ".$str.$filterSqlStr;
//echo $sql;
$request=$conndb->query($sql);
if($request){
/*echo "<script> window.location.href='download.php?id=".$crawlId."&file=".$saveFile."';</script>";*/
}else{
echo "<script> alert('下载失败');</script>";
exit;
} //删除临时文件
$tmpFiliName=$filePath."tmp".$fileName;//临时标志文件
echo $tmpFiliName;
if(file_exists($tmpFiliName)){
unlink($tmpFiliName);
} // 此处给出你下载的文件名
// $file = fopen($filePath.$fileName, "r"); //打开文件
// //输入文件标签
// header("Content-type:application/octet-stream ");
// header("Accept-Ranges:bytes ");
// header("Accept-Length: " . filesize($filePath.$fileName));
// header("Content-Disposition: attachment; filename= ".$fileName);
//
// //输出文件内容
// echo fread($file, filesize($filePath . $fileName));
// fclose($file); ?>
进度条:downloadfile.php
<?php
header('Content-Type:text/html;charset=utf-8');
//$filePath="/minbao/data/amazon/data/";
$filePath="d:/www/amazon/data/";
$curcount_pricelimit = $_GET['curcount_pricelimit'];
$fileName = $_GET['fileName'];
$tmpFiliName=$filePath."tmp".$fileName;//临时标志文件 $crawlId=$_GET['id'];
$price=$_GET['price'];//最低价格
$name=urldecode($_GET['name']); //防止执行超时
set_time_limit(0);
//清空并关闭输出缓存
ob_end_clean();
//需要循环的数据
if ($curcount_pricelimit/10000>0){
$totalnumproduct=$curcount_pricelimit/10000;
}else{
$totalnumproduct=10;
}
for($i = 0; $i < $totalnumproduct; $i++)
{
$users[] = $fileName.'_' . $i;
}
//计算数据的长度 $total = count($users);
//显示的进度条长度,单位 px
$width = 500;
//每条记录的操作所占的进度条单位长度
$pix = $width / $total;
//默认开始的进度条百分比
$progress = 0;
?>
<html>
<head>
<title>下载提示</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="./js/jquery-1.8.3.min.js"></script>
<style>
body,div input {
font-family: Tahoma;
font-size: 9pt
}
</style>
<script language="JavaScript">
<!--
function updateProgress(sMsg, iWidth,fsize)
{
//$("#downloaded").innerHTML=fsize+"KB";
//alert(fsize);
document.getElementById("downloaded").innerHTML =fsize+"KB";
document.getElementById("status").innerHTML = sMsg;
document.getElementById("progress").style.width = iWidth + "px";
document.getElementById("percent").innerHTML = parseInt(iWidth / <?php echo $width; ?> * 100) + "%";
}
--> function setDownloaded(fsize) {
fsize=fsize/1024;
fsize=Math.round(fsize*100)/100;
//alert(fsize);
document.getElementById("downloaded").innerHTML =fsize+"KB";
} </script>
</head>
<body> <table border="1" width="300">
<tr>
<td>已经生成</td>
<td><div id="downloaded">0</div></td>
</tr>
</table> <div>
<div style="margin:50px auto; padding: 8px; border: 1px solid gray; background: #EAEAEA; width: <?php echo $width+8; ?>px">
<div style="padding: 0; background-color: white; border: 1px solid navy; width: <?php echo $width; ?>px">
<div id="progress"
style="padding: 0; background-color: #FFCC66; border: 0; width: 0px; text-align: center; height: 16px"></div>
</div>
<div id="status"></div>
<div id="percent"
style="position: relative; top: -30px; text-align: center; font-weight: bold; font-size: 8pt">0%</div>
</div>
<?php echo $tmpFiliName."<br />";
if(!file_exists($tmpFiliName)){
echo "创建文件<br />";
file_put_contents($tmpFiliName,'tmpContent') or die("Unable to open file!");
if(file_exists($tmpFiliName)){
echo "创建成功<br />";
$url="download.php?id=".$crawlId."&name=".$name."&price=".$price."&fileName=".$fileName;
echo $url."<br />";
echo "<script>$.ajax('".$url."');</script>";
}
}else{
echo "临时标志文件已经存在!";
} flush(); //将输出发送给客户端浏览器
$filesize=0;
foreach($users as $user)
{
// 在此处使用空循环模拟较为耗时的操作,实际应用中需将其替换;
// 如果你的操作不耗时,我想你就没必要使用这个脚本了 :)
$cnt=0;
if(file_exists($tmpFiliName)){ if(file_exists($filePath.$fileName)){
$file = fopen($filePath.$fileName, "rb");
if ($file) {
$filesize=filesize($filePath.$fileName);
clearstatcache();
// echo $cnt++."、文件大小:".($filesize/1024)."KB<br>";
echo "<script>setDownloaded($filesize);</script>";//在前台显示已经下载文件大小
}
}
}else{
if(file_exists($filePath.$fileName)){
$file = fopen($filePath.$fileName, "rb");
if ($file) {
$filesize=filesize($filePath.$fileName);
clearstatcache();
// echo $cnt++."、文件大小:".($filesize/1024)."KB<br>";
echo "<script>setDownloaded($filesize);</script>";//在前台显示已经下载文件大小
}
}
break;
}
//for($i=0;$i<1000000;$i++){ //}
flush();
sleep(1);
?>
<script language="JavaScript">
updateProgress("正在生成数据文件 <?php echo $user; ?> ....", <?php echo min($width, intval($progress)); ?>,<?php echo $filesize/1024;?>);
</script>
<?php
flush(); //将输出发送给客户端浏览器,使其可以立即执行服务器端输出的 JavaScript 程序。
$progress += $pix;
} //end foreach ?>
<script language="JavaScript">
//最后将进度条设置成最大值 $width,同时显示操作完成
updateProgress("数据文件生成完成!3秒后启动下载...", <?php echo $width; ?>,<?php echo round($filesize/1024,2);?>);
</script>
<?php
flush();
sleep(3);
?>
<script language="JavaScript">
window.location.href ="downloadfinish.php?filePath=<?php echo $filePath; ?>&fileName=<?php echo $fileName; ?>";
</script>
</script>
<?php
flush();
//删除临时文件
if(file_exists($tmpFiliName)){
unlink($tmpFiliName);
}
?>
</body>
</html>
完成下载:downloadfinish.php
<?php
$filePath=$_GET['filePath'];
$fileName=$_GET['fileName'];
//此处给出你下载的文件名
$file = fopen($filePath.$fileName, "r"); //打开文件
//输入文件标签
header("Content-type:application/octet-stream ");
header("Accept-Ranges:bytes ");
header("Accept-Length: " . filesize($filePath.$fileName));
header("Content-Disposition: attachment; filename= ".$fileName); //输出文件内容
echo fread($file, filesize($filePath . $fileName));
fclose($file);
?>