利用Jquery的AjaxUpload组件实现头像异步上传并回显
来源:互联网 发布:手机网络被劫持 编辑:程序博客网 时间:2024/05/03 12:40
练习中遇到头像上传不能实时显示的问题,利用ajaxUpload异步文件上传解决。
1、导入jquery-1.10.2.min.js、ajaxupload.3.6.js包。
附ajaxupload.3.6.js下载地址:http://download.csdn.net/detail/dengchenlu/3957758
2、jsp中导入包
<script type="text/javascript" src="${basePath }js/jquery/ajaxupload.3.6.js"></script>
2、jsp中的配置
js:
//利用AjaxUpload组件实现异步上传头像并回显 $(document).ready(function(){ var button = $("#imgButton"); new AjaxUpload(button,{ action:"${basePath}nsfw/user_ajaxUpload.action", name:"headImg",//同Action中File文件名,""不能少 onSubmit: function(file, ext) { if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF)$/.test(ext))) { alert("您上传的图片格式不对,请重新选择!"); return false; } }, onComplete:function(file,response){//默认全成功,不再判断,直接设置img的src var path = "${basePath}upload/"; var reg = /<pre.+?>(.+)<\/pre>/g; var result = response.match(reg); response = RegExp.$1;//以上三行是为了去除response返回的pre标签内容 $("#headImg").attr("src",path+response); //由于已经异步设置了头像路径,注册时不再需要重新设置,因此直接隐藏域保存headImg属性 $("#imgHidden").val(response); } }); });
html:
<td class="tdBg" width="200px">头像:</td> <td> <img id="headImg" src="${basePath }upload/<s:property value='user.headImg'/>" width="100" height="100"/> <input id="imgButton" type="button" name="headImg" value="上传头像" class="btn32"/> <s:hidden name="user.headImg" id="imgHidden"/> </td>Action中的接受配置:
//异步显示头像public void ajaxUpload(){try {//处理用户头像if(headImg != null){String filePath = ServletActionContext.getServletContext().getRealPath("upload/user");//保存路径//保存文件名 uuid+后缀String fileName = UUID.randomUUID().toString().replace("-", "")+headImgFileName.substring(headImgFileName.lastIndexOf('.'));FileUtils.copyFile(headImg,new File(filePath,fileName));String headImg = "user/"+fileName;//user/开头HttpServletResponse response = ServletActionContext.getResponse();response.setCharacterEncoding("text/html;charset=UTF-8");response.setCharacterEncoding("UTF-8");response.getWriter().print(headImg);}} catch (Exception e) {e.printStackTrace();}}File及setter,getter
//头像private File headImg;private String headImgFileName;private String headImgContentType;最终效果:
0 0
- 利用Jquery的AjaxUpload组件实现头像异步上传并回显
- 如何使用ajaxUpload方式实现异步上传
- jquery ajaxupload实现打开文件夹上传指定位置的文件
- jquery插件ajaxupload实现文件上传操作
- laravel博客开发之利用jcorp结合laravel和ajaxupload实现用户头像上传和裁剪功能
- ajaxupload组件上传图片
- 基于jquery的上传文件插件ajaxUpload
- 利用jquery.form实现异步上传文件
- java利用jquery和smartupload实现头像上传切割效果
- Jquery AjaxUpload实现文件上传功能代码实例教程
- 异步实现springboot+thymeleaf上传头像的实现
- AjaxUpload上传控件、JQuery控件
- 利用铁匠组件实现在线头像截图的效果
- 实现头像的上传
- cropper配合ajaxupload实现图片截图与上传并上传至远程服务器地址
- jquery上传控件ajaxupload传递参数
- 利用js实现无刷新上传头像(或文件)并显示进度条
- springmvc+ajaxfileupload实现头像上传并预览
- 泊松图像编辑(Poisson image editing)
- 避免本地和服务器不同步
- Redis常用API-使用文档
- PHP拓展安装
- 字符串函数
- 利用Jquery的AjaxUpload组件实现头像异步上传并回显
- 深入Android Handler源码,分析主线程、子线程通讯原理
- MySQL入门之基础知识
- Python练手项目0011
- Python爬虫:抓取手机APP数据
- 链表合并
- RelativeLayout
- mac修改hosts
- 流媒体开发之--HLS--M3U8解析(2): HLS草案