最好用的AJAX异步上传程序

前端之家收集整理的这篇文章主要介绍了最好用的AJAX异步上传程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP部分=========================================

$time = time();//定义变量,保存图片名,以防图片的名字相同
$suffix = strrchr($_FILES["file"]["name"],".");//获取图片的后缀名
  
if($suffix!='.txt'){echo "只能上传txt格式";}
$upname =$time.$suffix;//上传文件名称

$upfile = "./up/".$upname;//上传文件的保存位置
$tmp_name=$_FILES["file"]["tmp_name"];//上传文件的临时目录
 
if($_FILES["file"]["error"]>0){
    echo "上传文件有误:".$_FILES["file"]["error"];
   
}else{
    if(file_exists($upfile)){
        echo  "已经存在";
    }else{
        if(move_uploaded_file($tmp_name,$upfile)){//将临时文件转移到制定目录下 
			$fp=fopen($upfile,'r');
			while(!feof($fp)){
				$buffer=fgets($fp,4096);
				$haoma=str_replace("\n",",str_replace("\r\n",$buffer));
				$arr_haoma=array();
				$arr_haoma=explode(",$haoma);
				for($i=0;$i<count($arr_haoma);$i++){
					
				}
			}
			fclose($fp);
           // echo  $fp;//上传成功
           
        }else{
            echo "上传失败";//上传失败
           
        }
         
    }
}

HTML部分====================================================
<form method="post" action="" enctype="multipart/form-data" name="theForm" onsubmit="return addbooks();">
<div class="uptxt f">
	<dl><dt></dt><dd>每行一个号码,或者用","符号隔开</dd></dl>
	<dl><dt></dt><dd>最大上传<span class=fls>3000KB</span>,最多上传<span class=fls>200,000</span>个号码</dd></dl>
	<dl><dt></dt><dd><input type="file" id='file' name='file' class=inputk style="width:190px;*width:200px;"></dd></dl>
	<dl><dt></dt><dd><input type="hidden" name="mid" id=mid value=""> <input type="submit" name=submit value="提交" class=input0></dd></dl>
</div><div class=clear></div><div class=gap></div><div class=gap></div>
</form>

JS部分=======================================================
<script>
function addbooks(){
	var mid=$("#mid").val();
	//$.weeBoxs.open(WorkLoad,{title:WorkTips,showClose:false,showButton:false,Boxid:'adddao'});
		var s = 0;
		$.weeBoxs.open(WorkLoad + "用时:<span id='runtime'>" + s + "</span>秒",{
			title: WorkTips,showClose: false,showButton: false,Boxid: 'loadtip'
		});
		var wait = setInterval(function() {
			s++;
			$("#runtime").html(s);
		},1000);
		$.ajaxFileUpload({
				url:'/js/ups.PHP',cache: false,secureuri:false,fileElementId:'file',dataType: 'data',type: "POST",data:"mid="+mid,success: function (msg){
					//alert(msg);
					clearInterval(wait);
					if(msg==1){
						sms('sms','sms_phone_list','phone_list',0);
						$.weeBoxs.close();
						$.weeBoxs.close();
					}else{
						alert(msg);
						$.weeBoxs.close();
						$.weeBoxs.close();
					}
					//if(m.msg=='no'){
//						$.weeBoxs.open(WorkError+"没有选择文件!",Boxid:'adddao'});
//					}else if(m.msg=='hit'){
//						$.weeBoxs.open(WorkError+"上传号码超过限制!",Boxid:'adddao'});
//					}else if(m.msg=='size'){
//						$.weeBoxs.open(WorkError+"上传文件大小超过限制!",Boxid:'adddao'});
//					}else if(m.msg=='error'){
//						$.weeBoxs.open(WorkError+"只能上传txt格式!",Boxid:'adddao'});
//				  }else{
//				  	sms('sms',0);
//						$.weeBoxs.close();
//						$.weeBoxs.close();
//					}
					
				},error: function(XMLHttpRequest,textStatus,errorThrown) {

				clearInterval(wait);
				$.weeBoxs.close();
				
			$.weeBoxs.open(WorkError + XMLHttpRequest,{
				title: WorkTips,showCancel: false,Boxid: 'tip'
			});

		}

			});
	return false;
}
</script>

最后记得引用JS

<script src='/m/js/ajaxfileupload.js' type=text/javascript></script>

原文链接:https://www.f2er.com/ajax/166125.html

猜你在找的Ajax相关文章