HTML5 FileReader多图预览 Ajax多图异步上传ThinkPHP接收

本文介绍了如何使用HTML5的FileReader接口实现多图预览,结合Ajax和formData实现多图异步上传到ThinkPHP后台。虽然示例效果简洁,但能有效实现功能,并提醒读者可以优化展示效果,同时注意不同ThinkPHP版本的上传类实例化差异。

最近在做一个多图上传的功能,就想试试用html5的FileReader接口来实现上传前的预览,利用FormData包装多张图片,提交给ThinkPHP后台,找了一些网上的例子,然后自己根据实际需求改了一下,实现了以下效果:


 做的效果不是特别好看,请谅解!有想法的人,可以去把效果做得更好,我这里就是为了简单地实现出功能,也没有仔细测试过浏览器兼容

<div id="imgesFilesAuto" style="display:none;">
         <div id="imagesBox"></div>
      	 <div id="imgesFiles">
            <span>+</span>
            <input type="file" name="photoimage" id="uploadImage" multiple="multiple" style="width:170px; height:250px;opacity:0;" accept="image/gif, image/jpeg"/>
         </div>
      </div>

#imgesFilesAuto{
	width:auto;
	height:200px;
	border:#CCC 1px solid;
}
#imagesBox{
	width:auto;
	height:198px;
	float:left;
}
.imagesItem{
	width:170px;
	height:198px;
	float:left;
	background-position: center center;
    background-size: cover;
    -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
}
#imgesFiles{
	width:170px;
	height:198px;
	float:left;
	position:relative;
	border-right:#CCC 1px solid;
}
#imgesFiles span{
	position:absolute;
	font-size:126px;
	display:block;
	width:170px;
	text-align:center;
	height:198px;
	color:#CCC;
}

var index = 1;
var fromdata = new FormData();
$("#uploadImage").on("change", function(){
	
	//var mFiles = new FromData();
    // Get a reference to the fileList
    var files = !!this.files ? this.files : [];
    // If no files were selected, or no FileReader support, return
    if (!files.length || !window.FileReader) return;
    // Only proceed if the selected file is an image
    if (/^image/.test( files[0].type)){
        // Create a new instance of the FileReader
        var reader = new FileReader();
        // Read the local file as a DataURL
        reader.readAsDataURL(files[0]);
		
		fromdata.append("photo"+index, files[0]);
        // When loaded, set image data as background of div
        reader.onloadend = function(){
       $("#imagesBox").show();
	   $("#imagesBox").append("<div class='imagesItem' id='imagesItem"+index+"'></div>");
       $("#imagesItem"+index).css("background-image", "url(https://blog.csdn.net/"+this.result+")");
         index++;
        }
  
    }
  
});

$("#btn1").click(function(){
	  //alert("__URL__");
	  $.ajax({
		  url: "__URL__/addInformation",
		  type: "POST",
		  data: fromdata,
		  processData: false,  // 告诉jQuery不要去处理发送的数据
		  contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
		  success:function(data){
                        console.log(data);
                    },
          error:function(XmlHttpRequest,textStatus,errorThrown){
                        console.log(XmlHttpRequest);
                        console.log(textStatus);
                        console.log(errorThrown);
                    }
		});
	
});

ThinkPHP3.2版本  接收接口addInformation():

$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Public/Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传(子)目录
	    // 上传文件 
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	        $this->success('上传成功!');
	    }

根据ThinkPHP3.2文档里的对 FromData进行数据的组装


用其他ThinkPHP版本的,实例化上传类的方式有点区别,请自行调节


最后贴一张上传成功后的目录


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值