前台调用: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+"&>进度条和下载文件,创建标志文件 //$.ajax("download.php? + 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 >; $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?> }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); ?>