ajax无刷新上传文件

1.表单

 

   <form id="uploadform" action="/shareres/upload.win"  encType="multipart/form-data"   method="post" target="hidden_frame" >
				<iframe name="hidden_frame" id="hidden_frame" style="display:none"></iframe>              <tr>                <td style="font-weight:bold; border-bottom: #c1dfbe solid 3px; height:35px; line-height:35px;">填写文档信息</td>
              </tr>
              <tr>
                <td style="height:35px; line-height:35px;">标题:<input type="text" name="docname" size="58" id="docname"/></td>
              </tr>
              <tr>
                <td><span style="float:left;">介绍:</span><textarea style="height:100px; width:413px; overflow:auto;" name="comment" id="comment"></textarea></td>
              </tr>
              <tr>
                <td style="height:35px; line-height:35px;">分类:{$cate1}{$cate2}</td>
              </tr>
              <tr>
                <td style="font-weight:bold; border-bottom: #c1dfbe solid 3px; height:35px; line-height:35px;">从电脑中选择要上传的文档</td>
              </tr>
              <tr>
                <td style="height:35px; line-height:35px;"><input type="file" size="50" name="resourcefile" id="file"/></td>
              </tr>
              <tr>
			  
                <td style="height:35px; line-height:35px;"><input type="button" value="开始上传" style="background:#99c521; height:25px; width:85px; border:0;" name="uploadbtn" id="uploadbtn" /></td>
              </tr>

2.用jquery提交表单

 

$("#uploadbtn").click(function(){
		if($("#docname").val()==""){
			alert("资源标题不能为空");
			return ;
		}else if($("#comment").val()==""){
			alert("资源介绍不能为空");
			return;
		}else if($("#file").val()==""){
			alert("请选择您要上传的文件");
			return ;
		}else{
			var ext=/\.[^\.]+$/.exec($("#file").val()); 
		
			if(!(ext==".doc"||ext==".ppt"||ext==".xls"||ext==".rar")){
				alert("您上传的文件格式不正确");
				return;
			}else{
				$("#uploadform").submit();
				
			}
		}
		
	
				
		});	

3.php上传文件类,网上找的

 

class upload_file{
		private	$upfile_type;
		private  $upfile_size;
		private 	$upfile_name;
		private 	$upfile;
		private	$d_alt;
		private 	$extention_list;
		private	$tmp;
		private 	$arri;
		private 	$datetime;
		private 	$date;
		private 	$filestr;
		private  $size;
		private	$ext;
		private	$check;
		private	$flash_directory;
		private	$extention;
		private	$file_path;
		private	$base_directory;
		private  $uploadstate;
		private  $savepath;
		
		//var $url; //文件上传成功后跳转路径;
 
	  function upload_file(){
		   //$this->set_url("index.php");          //初始化上传成功后跳转路径;
		   $this->set_extention();             //初始化扩展名列表;
		   $this->set_size(2048);              //初始化上传文件KB限制;
		   $this->set_date();               //设置目录名称;
		   $this->set_datetime();             //设置文件名称前缀;
		   $this->set_base_directory(_ATT_ROOT."resource");  //初始化文件上传根目录名,可修改!;
	  }
 
	//set方法
	  function set_file_type($upfile_type){
			$this->upfile_type = $upfile_type;       //取得文件类型;
	  }
 
	  function set_file_name($upfile_name){
			$this->upfile_name = $upfile_name;       //取得文件名称;
	  }
 
	  function set_upfile($upfile) {
			$this->upfile = $upfile;            //取得文件在服务端储存的临时文件名;
	  }
	   
	  function set_file_size($upfile_size){
			$this->upfile_size = $upfile_size;       //取得文件尺寸;
	  }
 
	  function set_url($url){
			$this->url = $url;               //设置成功上传文件后的跳转路径;
	  }
	 
	  function get_extention(){
			$this->extention = preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$this->upfile_name); //取得文件扩展名;
	 }
     
 
	  function set_datetime(){
			$this->datetime = date("YmdHis");        //按时间生成文件名;
	  }
	 
	 
	  function set_date(){
			$this->date = date("Ymd");          //按日期生成目录名称;
	  }
	 
	 
	  function set_extention(){
			$this->extention_list = "doc|xls|ppt|rar"; ///默认允许上传的扩展名称;
	  } 
	 
	 
	  function set_size($size){
			$this->size = $size;              //设置最大允许上传的文件大小;
	  }
	 
	 
	  function set_base_directory($directory){
			$this->base_directory = $directory; //生成文件存储根目录;
	  }
	 
	 
	  function set_flash_directory(){
			$this->flash_directory = $this->base_directory."/".$this->date; //生成文件存储子目录;
	  }

 
	  function showerror($errstr="未知错误!"){
			echo "<script language=javascript>alert('$errstr');location='javascript:history.go(-1);';</script>";
			exit();
	  }
	 
	 
	  function go_to($str,$url){
			echo "<script language='javascript'>alert('$str');location='$url';</script>";
			exit();
	  }

	 
	  function mk_base_dir(){
		   if (! file_exists($this->base_directory)){   //检测根目录是否存在;
				@mkdir($this->base_directory,0755);     //不存在则创建;
		   }
	  }

	 
	  function mk_dir(){
		   if (! file_exists($this->flash_directory)){   //检测子目录是否存在;
			@mkdir($this->flash_directory,0755);     //不存在则创建;
		   }
	  } 
	 
	 
	  function get_compare_extention(){
			$this->ext = explode("|",$this->extention_list);//以"|"来分解默认扩展名;
			return $this->ext ;
	  }
	 
	 
	  function check_extention(){
	
		   for($i=0;each($this->ext);$i++) {           //遍历数组;
				if($this->ext[$i] == strtolower($this->extention)){ //比较文件扩展名是否与默认允许的扩展名相符;
					$this->check = true;               //相符则标记;
					break;
				}
		   }
		   if(!$this->check){$this->showerror("正确的扩展名必须为".$this->extention_list."其中的一种!");}
		   //不符则警告
	  }
	 
	 
	  function check_size(){
		   if($this->upfile_size > round($this->size*1024)) {     //文件的大小是否超过了默认的尺寸;
				$this->showerror("上传附件不得超过".$this->size."KB"); //超过则警告;
		   }
	  }

	 
	  function set_file_path(){
				$this->file_path = $this->flash_directory."/".$_SESSION['userid']."-".$this->datetime.".".$this->extention; //生成文件完整访问路径;
				$this->savepath="/resource//".$this->date."/".$_SESSION['userid']."-".$this->datetime.".".$this->extention;
	  }
	  function get_file_path(){
				return $this->file_path;
	  }
	 
	  function copy_file() {
		   if(copy($this->upfile,$this->file_path)){        //上传文件;
					$this->uploadstate=1;
					echo "<script language=javascript>alert('文件已经成功上传!');</script>";
				
		   }else {
					print $this->showerror("意外错误,请重试!");     //上传失败;
		   }
	  }
	 
	 
	  function save(){
		   $this->set_flash_directory();  //指定上传子目录
		   $this->get_extention();     //获取扩展名
		  
		   $this->get_compare_extention(); //分解扩展名
		 
		   $this->check_extention();    //检查文件扩展名
		   
		   $this->check_size();      //检查文件尺寸
		   $this->mk_base_dir();      //如果父目录不存在创建父目录
		   $this->mk_dir();        //如果子目录不存在创建子目录
		   $this->set_file_path();     //生成文件完整访问路径
		   $this->copy_file();       //上传文件
	  }
		
	 }

 

你可能感兴趣的:(Ajax,无刷新)