最近发现网上转载不注明出处的文章很多,为了创造一个良好的开源环境.请您转载后注明出处.谢谢合作!这样会鼓励我们的开源欲望.
jquery 这个JS组件不知道大家用过没有?在有一定的Ajax基础之后,利用它来开发Ajax是一件非常有趣的事情,一直以来就被Js的传统编程环境所烦的.它可以简化我的的JS 代码,加快你们的开发速度,代码一目了然.一但有了自己的编程习惯之后,那可以说会如鱼得水.
今天利用Jquery 的File Upload 组件,再结合Php那简单的文件上传组件,一个非常 高效的Ajax文件上传工具就搞定啦.可以实现我们一般的程序要求,下面就和大家分享一下,免得和我一样在网上找来找去都没得一个实用的,最后还得自己写一个.~_~
效果图如下:
JS代码:
<script type="text/javascript" language="javascript"> function ajaxFileUpload() { $("#loading") .ajaxStart(function(){ $(this).show(); }) .ajaxComplete(function(){ $(this).hide(); }); $.ajaxFileUpload ( { url:'up.php', secureuri:false, fileElementId:'fileToUpload', dataType: 'html', success: function (data) { alert(data); }, error: function (data, status, e) { alert(e); } } ) return false; } </script>Php文件上件处理代码:#--time:2008.07.10--#
#--anthor:fkedwgwy--#
#--:37304662--#
#--blog:http://blog.youkuaiyun.com/fkedwgwy--#
#--欢迎您加入PHP开源的殿堂.--#
function uploadFile($file,$isImage=false){
set_time_limit(0);
$file_name=$file['name'];
$file_type=$file['type'];
$file_tmpname=$file['tmp_name'];
$file_size=$file['size'];
$file_error=$file['error'];
if(!empty($file_name)){
if($isImage==true){
if ($file_error==UPLOAD_ERROR_OK) {
if ($file_type=="image/gif"||$file_type=="image/jpg"||$file_type=="image/pjpeg"||$file_type=="image/jpeg"||$file_type=="image/x-png")
{
$upload_path="file/";
$upload_name=strrchr($file_name,".");
$upload_name=date("YmdHiss").$upload_name;
$upload_path=$upload_path.$upload_name;
if(move_uploaded_file($file_tmpname,$upload_path)){
return $upload_path;
}else {
$msg="上传失败";
}
}else {
$error="图片格式不对";
}
}else {
$error=$file_error;
}
}else {
if ($file_error==UPLOAD_ERROR_OK) {
$upload_path="file/";
$upload_name=strrchr($file_name,".");
$upload_name=date("YmdHis").$upload_name;
$upload_path.=$upload_name;
if(move_uploaded_file($file_tmpname,$upload_path)){
return $upload_path;
}else {
$msg="上传失败";
}
}
}
}else {
if($isImage==true){
$error="请选择你要上传的图片";
}else {
$error="请选择你要上传的软件";
}
}
}
html部分:
<table cellpadding="0" cellspacing="0" class="tableForm">
<thead>
<tr>
<th>
fkedwgwy-Ajax_Php_file_upload</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input id="fileToUpload" type="file" size="45" name="fileToUpload"></td>
</tr>
<tr>
<td>
请选择文件上传(PHP家园:37304662)</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>
<button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">
Upload</button>
<a href="http://blog.youkuaiyun.com/fkedwgwy">http://blog.youkuaiyun.com/fkedwgwy</a></td>
</tr>
</tfoot>
</table>
原代码下载地址:http://download.youkuaiyun.com/source/533941