(大文件上传方案)大文件上传SWFUpload汉化修改版
版权所有:SWFUpload、小万玩玩 [转载请注明出处,谢谢!
汉化修改使用版本:SWFUpload v2.2.0.Beta2 Core.zip
官网最新版本:SWFUpload v2.2.0.1 Core.zip (貌似更新不是很多)
下载:http://code.google.com/p/swfupload/downloads/list
(2009-04-13)加入了判断队列文件总大小,并与传入的可用空间做对比。多用户上传时可用来做限制。
无论文件是否正在上传还是准备上传,cancelUpload后始终会调用uploadError,但怎么说明文档里只提到前者?害我搞了半天,解决方案这里:http://swfupload.org/forum/generaldiscussion/350
(2009-04-11)看到有网友在进度信息条那里做了些修改,显示了进度,速度,剩余时间,特添加进来,感谢这位网友的分享!
http://hi.baidu.com/it_security/blog/item/d8f3ed08f8314384d0581bc6.html
有热心网友发现在FF下运行不正常,报404、500。原来upload_url路径有问题,奇怪了,IE下能自动解析相对路径,FF却要写绝对路径,可恶的FF。顺便解决了几个bug:
1。支持FF。
处理方法:swfup_file.php:
upload_url:"<?php echo dirname("http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'])."/upload.php";?>",
2。解决当$renamed="0"时(即不自动重命名),含有中文的文件名出现乱码。
处理方法:upload.php:
$upload_file['filename']=iconv("utf-8","gbk",$upload_file['filename']);
3。swfup_file.php报错:Undefined index。(上传时报错:500)
处理方法:php.ini:
display_errors = Off
error_reporting = E_ALL & ~E_NOTICE
4。Win2003+IIS+PHP+fastcgi上传大文件(超过10M)会返回I/O或500错误。
处理方法:修改FastCgi的配置文件“fcgiext.ini”,“ c:\windows\system32\inetsrv\fcgiext.ini ”
[Types]
php=PHP
[PHP]
ExePath=C:\PHP\php-cgi.exe
InstanceMaxRequests=10000
EnvironmentVars=PHP_FCGI_MAX_REQUESTS:10000
RequestTimeout=500
ActivityTimeout=900
参考资料:http://www.wbphp.cn/html/y2008/409.html
http://hi.baidu.com/lecherline/blog/item/b553b70a627f7f1895ca6bc4.html
http://www.imdao.net/blog/read.php?123
2008-12-04
最近有比较多网友问我要源码,原来QQ空间相片上传那里也是用swfupload做的,只不过改了下界面,添加了些代码而已。
网友应用案例:www.jksing.com(2008-11-08)、www.wbxxg.com(2008-11-13)
swfupload官网的最新文档里愕然出现"Deprecated. Not compatible with Flash Player 10."的字样,怪不得前两天制作的汉化修正版不能在firefox下跑,甚至有些IE也跑不了,TNND,都是flashplayer惹的祸!不知macromedia被adobe收购后会是怎样的未来??
1。对官网的demo只做了少量的修改,便于升级
2。制作成单独页面swfup_file.php,方便多次调用
3。美化了界面
修改及优势:
这次我拿swfupload的最新demo直接修改,改过的部分如下:
1。fileprogress.js
function FileProgress(file, targetID)的末尾和FileProgress.prototype.disappear = function ()的头部加入:
validateForm();//调用父框架的函数,动态改变框架页大小
2。swfupload.js
this.ensureDefault("button_action", SWFUpload.BUTTON_ACTION.SELECT_FILES);
SWFUpload.BUTTON_ACTION.SELECT_FILES //支持多文件选择
改为SWFUpload.BUTTON_ACTION.SELECT_FILE //单文件选择
3。handles.js
(1)。function fileQueued(file) 加入:
var txtFileName = document.getElementById("txtFileName");
txtFileName.value = file.name; //置文本框的值为刚才选择的文件名
(2)。function fileDialogComplete(numFilesSelected, numFilesQueued)
注释掉://this.startUpload(); //注释掉表示选择文件后不自动开始上传
(3)。function uploadComplete(file) 改为://上传完成后,包括出错和成功
function uploadComplete(file) {
if(this.getStats().successful_uploads==this.settings.file_upload_limit){ //如果已上传的个数等于限制个数
this.setButtonDisabled(true); //浏览控件置不可用
document.getElementById( this.customSettings.uploadButtonId).disabled = true;//浏览控件置不可用
}
if (this.getStats().files_queued === 0) {}
}
(4)。function queueComplete(numFilesUploaded) 加入: //当前上传队列结束后
alluploadDone();//所有文件上传成功
(5)。function uploadError(file, errorCode, message),加入 //上传出错时
this.setButtonDisabled(false); //浏览控件置可用
document.getElementById(this.customSettings.uploadButtonId).disabled = false; //上传控件置可用
(6)。修改uploadStart、uploadProgress、uploadSuccess,加入详细进度信息
var iTime = ""; //intial time
var Timeleft = ""; //time left
function uploadStart(file) { //上传开始时,如果队列数和限制个数相等,则置控件不可用
try {
if (this.getStats().files_queued == this.settings.file_upload_limit) {
this.setButtonDisabled(true);//浏览控件置不可用
document.getElementById(this.customSettings.uploadButtonId).disabled = true;//上传控件不可用
}
//alert(this.getStats().successful_uploads);
//this.setButtonDisabled(true);
//document.getElementById(this.customSettings.uploadButtonId).disabled = true;
/* I don't want to do any file validation or anything, I'll just update the UI and
return true to indicate that the upload should start.
It's important to update the UI here because in Linux no uploadProgress events are called. The best
we can do is say we are uploading.
*/
//Capture start time
var currentTime = new Date();
iTime = currentTime;
//Set Timeleft to estimating
Timeleft = "计算中...";
var progress = new FileProgress(file, this.customSettings.progressTarget);
progress.setStatus("上传中...");
progress.toggleCancel(true, this);
}
catch (ex) {}
return true;
}
//roundNumber found via google
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
//minsec created by Daem0nX (03.29.08)
function minsec(time, tempTime) {
var ztime;
if (time == "m") {
ztime = Math.floor(tempTime/60);
if (ztime < 10) {
ztime = "0" + ztime;
}
} else if (time == "s") {
ztime = Math.ceil(tempTime % 60);
if (ztime < 10) {
ztime = "0" + ztime;
}
} else {
ztime = "minsec error...";
}
return ztime;
}
function uploadProgress(file, bytesLoaded, bytesTotal) { //上传成功后
try {
var currentTime = new Date();
var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
var progress = new FileProgress(file, this.customSettings.progressTarget);
progress.setProgress(percent);
var tempTime = 0;
//rndfilesize = round file size
var rndfilesize = roundNumber(((file.size/1024)/1024),1);
//uploaded = how much has been uploaded
var uploaded = roundNumber(((bytesLoaded/1024)/1024),1);
//uTime = uploadTime (time spent uploading)
var uTime = (Math.ceil(currentTime-iTime)/1000);
//uSpeed = uploadSpeed (40 kB/s)
var uSpeed = Math.floor(roundNumber(((bytesLoaded/uTime)/1024),2));
//tempTime = store time for following functions
var tempTime = uTime;
//uploadTime in min:sec
uTime = "用时" + minsec("m", tempTime) + "分:" + minsec("s", tempTime) + "秒";
//tempTime = reassign val
tempTime = roundNumber(((((bytesTotal-bytesLoaded)/uSpeed)/60)/10),2);
if (tempTime != "Infinity") {
if (tempTime > 0) {
//if greater than 0
//Timeleft in min:sec
Timeleft = minsec("m", tempTime) + "分:" + minsec("s", tempTime) + '秒';
} else {
Timeleft = "计算中...";
}
} else {
Timeleft = "计算中...";
}
progress.setStatus('<b><font color=red>' +uploaded + '</font></b>/' + rndfilesize + ' MB,上传速度: <b><font color=red>' + uSpeed + ' </font></b>KB/秒; 剩余时间: <b><font color=red>' + Timeleft + '</font></b>; 总进度 <b><font color=red>' + percent + '%</font></b>');
} catch (ex) {
this.debug(ex);
}
}
function uploadSuccess(file, serverData) {
try {
var currentTime = new Date();
var progress = new FileProgress(file, this.customSettings.progressTarget);
progress.setComplete();
//Calculate upload time
var cTime = (Math.ceil(currentTime-iTime)/1000);
var zmin = 0;
var zsec = 0;
zmin = Math.floor(cTime/60);
if (zmin < 10) {
zmin = "0" + zmin;
}
zsec = Math.ceil(cTime % 60);
if (zsec < 10) {
zsec = "0" + zsec;
}
//Show how long the upload took
progress.setStatus("上传完成,用时:<b><font color=red> " + zmin + "分:" + zsec + '秒</font></b>');
progress.toggleCancel(false);
if (serverData === " ") {
this.customSettings.upload_successful = false;
} else {
this.customSettings.upload_successful = true;
document.getElementById("hidFileID").value = serverData;
uploadDone();//单个文件上传成功
}
} catch (ex) {
this.debug(ex);
}
}
4。fileprogress.js
FileProgress.prototype.setComplete = function () //上传完成后不要隐藏标签
注释掉 //var oSelf = this;
//setTimeout(function () {
// oSelf.disappear();
//}, 10000);
5。swfup_file.php内自定义函数:
uploadDone() //单个文件上传成功
alluploadDone() //所有文件上传成功
validateForm() //JS文件会调用父框架ReSizeiFrame()
6。index.php内自定义函数:
ReSizeiFrame() //此函数来动态改变框架大小
文件简单介绍:
1.主页面
index.php
调用代码:
<?
include("swfupload/key.php"); //包含URL加密解密文件
?>
<input type="text" name="upload_file1" id="upload_file1" value="文件上传成功后,此处显示文件路径和名称" />
//列出上传成功的文件名
方式一:(点击按钮打开上传页)
<input name="button_c" type="button" id="button_c" onclick="win_open('swfup_file.php?url=<?=encrypt_url("types=rar|word|xls|ppt&size=100&limit=2&show_submit=1&return_obj=upload_file1",$key)?>','370','100');" value="在线上传" />
方式二:(页面嵌入上传页)
<iframe align="left" name="swfup_file1" id="swfup_file1" frameborder="0" scrolling="no" src="swfup_file.php?url=<?=encrypt_url("types=rar|word|xls|ppt&size=100&limit=2&show_submit=1&return_obj=upload_file1&a=400&u=100",$key)?>" height="100" onLoad="ReSizeiFrame(this)" width="100%"></iframe>
//嵌套上传页面swfup_file.php
//上传类型:types ;文件大小(M):size;文件个数:limit;是否在iframe页显示上传按钮:show_submit;嵌套iframe的标识:obj;接收上传文件名的对象:return_obj;
总空间大小:a;a=-1时代表不限制
已使用空间:u;若a!=-1,则a-u的值将对上传队列总大小进行限制
2.上传页
swfup_file.php
3.上传保存页
upload.php
此页实现文件的上传并重命名
4.URL参数加密页
key.php
为了保护iframe页面参数不被恶意修改,所以需要加密URL地址,加密函数:encrypt_url,解密函数:geturl,密钥是key.php中的$key变量。
调用方法:
请查看index.php文件
其它说明:
index.php中操作队列:
<a href=# onclick=javscript:alert(swf_upload_control.getStats().files_queued)>显示上传队列个数</a>
<a href=# onclick=javscript:cacelall()>取消所有队列</a>
<a href=# onclick=javscript:swf_upload_control.cancelUpload();uploadDone();>取消一个队列</a>
index.php中操作上传:
<a href="javascript:window.swfup_file1.document.getElementById('btnSubmit').click();">点击此处上传</a>
php.ini中的设置:(最大支持上传大小)
file_uploads = on ;是否允许通过HTTP上传文件的开关。默认为ON即是开
upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就会用系统默认的临时文件夹
upload_max_filesize = 600M ;望文生意,即允许上传文件大小的最大值。默认为2M
post_max_size = 600M ;指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。默认为8M
max_execution_time = 600 ; 每个PHP页面运行的最大时间值(秒),默认30秒
max_input_time = 600 ; 每个PHP页面接收数据所需的最大时间,默认60秒
memory_limit = 8M ;每个PHP页面所吃掉的最大内存,默认8M
display_errors = Off 屏蔽显示错误,例如变量未定义报错等
error_reporting = E_ALL & ~E_NOTICE