PHP 文件下载 显示进度条
前台调用: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);
?>
PHP 文件下载 显示进度条的更多相关文章
- python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)
先来回顾一下昨天的内容 黏包现象粘包现象的成因 : tcp协议的特点 面向流的 为了保证可靠传输 所以有很多优化的机制 无边界 所有在连接建立的基础上传递的数据之间没有界限 收发消息很有可能不完全相 ...
- 【转】C#中使用aria2c进行下载并显示进度条
[转自] C#中使用aria2c进行下载并显示进度条 - 云平台知识库 - 博客园https://www.cnblogs.com/littlehb/p/5782714.html 正则表达式的生成网站: ...
- 036_python的大文件下载以及进度条展示
复习 1.黏包现象 粘包现象的成因: tcp协议的特点,面向流的,为了保证可靠传输,所以有很多优化的机制. 无边界 所有在连接建立的基础上传递的数据之间没有界限. 收发消息很有可能不完全相等. 缓存机 ...
- 任务栏显示进度条 CreateComObject(CLSID_TaskbarList) as ITaskbarList4
http://www.cnblogs.com/jxsoft/archive/2011/06/02/2067712.html //在任务栏显示 进度条 unit Unit9; interface use ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
- WPF下载远程文件,并显示进度条和百分比
WPF下载远程文件,并显示进度条和百分比 1.xaml <ProgressBar HorizontalAlignment="Left" Height="10&quo ...
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
关于多线程执行显示进度条的实例! 之前回答了一篇关于怎么在线程中操作进度条的帖子,估计有人看的不是很明白今天没事,写了一个小小的实例,很简单,就2个文件权当抛砖引玉,希望有更好解决方案的人发表一下意见 ...
- android标题栏(titlebar)显示进度条
在后台线程中执行各种操作(网络连接.大数据存储)的时候,我们希望让客户能看到后台有操作在进行,那么既能有效的提示用户,又不占用当前操作空间,最好的方法就是在标题栏有个进度条. [代码] [Java]代 ...
- ProgressIndicator显示进度条以及一些文字信息
//ProgressIndicator可以显示进度条以及一些文字信息,不过这个属性一般都在cs文件中操作. private void PhoneApplicationPage_Loaded(objec ...
随机推荐
- 两年来的core折腾之路几点总结,附上nginx启用http2拿来即用的配置
序:一年多没更新博客园的内容了,core已经发生了翻天覆地的变化,想起2014年这时候,我就开始了从当时还叫k的那套preview都不如的vnext搭建这套系统,陆陆续续它每一次升级,我也相应地折腾, ...
- kubernetes单机板
参考地址: *** http://blog.csdn.net/carter115/article/details/51121223 ** http://www.cnblogs.com/dongdong ...
- 修改 jquery.validate.js 支持非form标签
尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...
- shell-for循环
sheel语言for循环格式 for var in item1 item2 ... itemN do command1 command2 ... commandN done 案例1 #!/bin/ba ...
- Nginx相关集合
http://www.cnblogs.com/kamil/p/5163182.html LNMP搭建(yum) Nginx基本使用 http://www.cnblogs.com/kamil/p/516 ...
- 生成GUID唯一值的方法汇总(dotnet/javascript/sqlserver)
一.在 .NET 中生成1.直接用.NET Framework 提供的 Guid() 函数,此种方法使用非常广泛.GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的任何两台计 ...
- django用户认证
利用django自带认证功能实现用户登录认证. views.py # Create your views here. from django.shortcuts import render_to_re ...
- UVa11427 Expect the Expected
数学期望 概率递推 每一天的概率都是独立且相同的.可以先推出每天打i盘赢j盘的概率f[i][j] f[i][j]=f[i-1][j]*(1-p) + f[i-1][j-1]*p 输 赢 设此人打一天胜 ...
- COGS732. [网络流24题] 试题库
«问题描述:假设一个试题库中有n道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性.现要从题库中抽取m 道题组成试卷.并要求试卷包含指定类型的试题.试设计一个满足要求的组卷算法.«编程任务: ...
- Beta阶段项目展示
1.团队简介 韩青长 前端工程师 我是韩青长,技术小白,抱着对软工的好奇和对未来工作的憧憬选了这门课.暂时选择了测试的工作,也对开发和UI有一定兴趣.从前上帝创造了我们,现在轮到我们来创造自己的软件了 ...