jqueryForm 异步上传图片文件
首先是HTML格式:
<li> <span>上传:</span> <div class="loginInput"> <div class="fileInput"> <div class="fileBox"> {if $info['certificate']} <img src="{$info['certificate']}" width="278"> {else} <i class="filePic"></i> <p>法人证书或营业执照复印件</p> {/if} </div> <input class="files" id="fileupload" name="file" type="file" value="{$info['certificate']}"/> <input type="hidden" name="certificate" id="pic_val" > </div> </div> </li>
input 的 name建议设置成file 要不有时候接收不到传递到后台的参数。
然后是js:
<script src="https://www.helloweba.com/demo/ajaxsubmit/jquery.form.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var bar = $('.bar'); var percent = $('.percent'); var showimg = $('#showimg'); var progress = $(".progress"); var files = $(".files"); var btn = $(".btn span"); $("#fileupload").wrap("<form id='myupload' action='{HOST_NAME}user/User/import3' method='post' enctype='multipart/form-data'></form>"); $("#fileupload").change(function(){ $("#myupload").ajaxSubmit({ dataType: 'json', beforeSend: function() { // showimg.empty(); // progress.show(); // var percentVal = '0%'; // bar.width(percentVal); // percent.html(percentVal); // btn.html("上传中..."); }, uploadProgress: function(event, position, total, percentComplete) { var percentVal = percentComplete + '%'; bar.width(percentVal); percent.html(percentVal); }, success: function(data) { if(data.status == 0) { $('.fileBox i').hide(); //删除 $('.fileBox p').hide(); // 删除 $('.fileBox img').attr('src', data.name); // 显示 $('.fileBox img').show(); //显示 $("#pic_val").attr('value', data.name); //传递图片值 // $("#show_pic_show").attr('src', data.name); // 传递到初始阶段 }else{ layer.msg(data.name, { offset: 't', anim: 6 }); return false; } return false; }, error:function(xhr){ btn.html("上传失败"); bar.width('0') files.html(xhr.responseText); } }); }); }); </script> <!--ajax图片上传结束-->
后台代码PHP:
// 上传文件 public function import3Action() { //上传头像 if(isPost()){ $file = $_FILES['file']['tmp_name']; if(!empty($file)){ Load::load_class('fileupload',DIR_BF_ROOT.'classes',0); $up = new fileupload; $up -> set("path", DIR_ROOT . 'static/uploadfile/zhizhao'); // $up -> set("path",DIR_BF_ROOT . 'excel/'); $up -> set("maxsize", 200000); $up -> set("allowtype", array("gif", "png", "jpg","jpeg")); // $up -> set("allowtype", array("xls")); if($up -> upload("file")) { // 获取上传后的文件名称 $filename = '/static/uploadfile/zhizhao/'.$up->getFileName(); echo json_encode(array('status' => 0, 'name' => $filename));exit(); } else { echo json_encode(array('statsu' => 1, 'name' => $up->getErrorMsg()));exit(); } } } //上传头像结束 include $this->display('fileTest.html'); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了