Home >PHP Framework >ThinkPHP >How does Thinkphp combine with ajaxFileUpload to implement asynchronous image transmission with ajax?
First introduce the jQuery and ajaxFileUpload plug-ins. Pay attention to the order. Needless to say, this is true for all plug-ins.
<script src="__ADMIN__/js/jquery.min.js?v=2.1.4"></script> <script src="__ADMIN__/js/ajaxfileupload.js"></script>
<div class="form-group"> <label class="col-sm-2 control-label">缩略图</label> <div class="col-sm-8"> <div id="file-pretty"> <div> <input type="file" id="file_thumb" name="thumb" class="form-control" value=""> <div class="input-append input-group"> <span class="input-group-btn"> <button id="btn_thumb" class="btn btn-white" type="button">选择图片</button> </span> <input id="info_thumb" name="thumb" class="input-large form-control" type="text" value="{$info.img}"> </div> </div> </div> </div> <div class="col-sm-2"><img id="show_thumb" src="/uploads/image/{$info.thumb}" alt=""></div> </div>
<script type="text/javascript"> $(function(){ $("#btn_thumb").click(function(){ $("#file_thumb").click(); }); //异步上传 $("body").delegate('#file_thumb', 'change', function(){ var filepath = $("input[name='thumb']").val(); var arr = filepath.split('.'); var ext = arr[arr.length-1]; //alert(filepath);exit(); if('gif|jpg|png|bmp'.indexOf(ext)>=0){ $.ajaxFileUpload({ url: '/admin/slide/upload_image', secureurl: false, fileElementId: 'file_thumb', //file标签ID dataType: 'json', //返回数据类型 data:{name:'thumb'}, success:function (data,status){ $("#info_thumb").val(data); $("#show_thumb").attr('src','/uploads/images/'+data); $("#info_thumb").focus(); }, complete:function (XMLHttpRequest){ }, error:function (data,status,e){ layer.alert('上传失败!'); }, }); } else { //清空file $("#file_thumb").val(""); layer.alert('请上传合适的图片类型!'); } }); }); </script>
//单文件(包含单文件)异步上传 public function upload_image(){ //图片上传 $file = request()->file(input('name')); $info = $file->move(ROOT_PATH . 'public/uploads/images'); if($info) { return json_encode($info->getSaveName()); } }
<div id="slideshow"> <ul class="rslides" id="slider"> {volist name="data" id="vo"} <li><a href="{$vo.url}" rel="external nofollow" rel="bookmark" target="_blank"> <img src="__UPLOADS__/images/{$vo.img}" max-width="" max-height="" alt="{$vo.title}"></a> <p class="slider-caption">{$vo.title}</p> </li> {/volist} </ul> </div>
The above is the detailed content of How does Thinkphp combine with ajaxFileUpload to implement asynchronous image transmission with ajax?. For more information, please follow other related articles on the PHP Chinese website!