使用jquery异步上传文件----SpringMVC注解开发
来源:互联网 发布:宝马etk软件下载 编辑:程序博客网 时间:2024/05/16 10:12
<span style="font-size:14px;color:#000000;background-color: rgb(204, 204, 204);"><script type="text/javascript"> //采用jquery.form.js异步上传图片,并结合<form>表单 function uploadFic(){ $("#jvForm").ajaxSubmit({ url : "${pageContext.request.contextPath}/upload/uploadPic.do", dataType : "json", type : "post", success : function(data){ //处理结果 //将相对路径设置给隐藏域中,提交时用 $("#imgUrl").val(data.path); //将全路径设置给img标签,显示图片用 $("#allImgUrl").attr("src",data.url); } }); }</script></span><strong></strong>
<span style="font-size:14px;"><div class="body-box" style="float:right"> <form id="jvForm" action="o_save.shtml" method="post" enctype="multipart/form-data"> <table cellspacing="1" cellpadding="2" width="100%" border="0" class="pn-ftable"> <tbody> <tr> <td width="20%" class="pn-flabel pn-flabel-h"> <span class="pn-frequired">*</span> 品牌名称:</td><td width="80%" class="pn-fcontent"> <input type="text" class="required" name="name" maxlength="100"/> </td> </tr> <tr> <td width="20%" class="pn-flabel pn-flabel-h"> <span class="pn-frequired">*</span> 上传商品图片(90x150尺寸):</td> <td width="80%" class="pn-fcontent"> 注:该尺寸图片必须为90x150。 </td> </tr> <tr> <td width="20%" class="pn-flabel pn-flabel-h"></td> <td width="80%" class="pn-fcontent"> <img src="" id="allImgUrl"/> <input type="hidden" name="imgUrl" id="imgUrl"/> <input type="file" name="uploadPic" onchange="uploadPic()"/> </td> </tr> <tr> <td width="20%" class="pn-flabel pn-flabel-h"> 品牌描述:</td><td width="80%" class="pn-fcontent"> <input type="text" class="required" name="name" maxlength="80" size="60"/> </td> </tr> <tr> <td width="20%" class="pn-flabel pn-flabel-h"> 排序:</td><td width="80%" class="pn-fcontent"> <input type="text" class="required" name="name" maxlength="80"/> </td> </tr> <tr> <td width="20%" class="pn-flabel pn-flabel-h"> 是否可用:</td><td width="80%" class="pn-fcontent"> <input type="radio" name="isDisplay" value="1" checked="checked"/>可用 <input type="radio" name="isDisplay" value="0"/>不可用 </td> </tr> </tbody> <tbody> <tr> <td class="pn-fbutton" colspan="2"> <input type="submit" class="submit" value="提交"/> <input type="reset" class="reset" value="重置"/> </td> </tr> </tbody> </table> </form></div></span>
<span style="font-size:14px;">@Controllerpublic class UploadController {@RequestMapping(value = "/upload/uploadPic.do")public void uploadBrandPic(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletResponse response,HttpServletRequest request){//图片名称生成策略---采用时间格式(精确到毫秒)并追加随机3位(10以内)数字//精确到毫秒DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");String picName = df.format(new Date());//随机再生成3位 10以内的数Random r = new Random();for(int i=0;i<3;i++){picName += r.nextInt(10);}//获取扩展名String originalFilename = file.getOriginalFilename();String ext = FilenameUtils.getExtension(originalFilename);//图片上传的路径String path = "upload/" + picName + "." + ext;//上传图片到指定位置记得先创建upload文件夹或是程序创建文件夹try {file.transferTo(new File(request.getSession().getServletContext().getRealPath("/") + path));} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}</span>
注意:需引入<script src="${pageContext.request.contextPath }/res/common/js/jquery.form.js" type="text/javascript"></script>文件
0 0
- 使用jquery异步上传文件----SpringMVC注解开发
- SpringMVC使用MultipartFile 实现异步上传文件
- springmvc异步上传文件
- SpringMVC-文件异步上传
- springmvc利用jquery.form插件异步上传文件
- springmvc结合jquery.form.js异步提交表单上传文件
- jquery异步上传文件
- jquery异步上传文件
- jquery异步上传文件
- jquery 异步上传文件
- springmvc 文件上传异步处理
- springmvc实现异步上传文件
- SpringMVC异步上传多文件
- SpringMVC AJAX异步文件上传
- springMVC----使用注解开发
- jquery实现文件异步上传
- jquery异步上传文件代码
- 使用jQuery.form插件实现表单异步提交+上传文件
- nginx + php-fpm fastcgi防止跨站、跨目录的安全设置
- js中使用jstl中得到的值
- Android编码规范
- YTU 2451: 股市风云
- 6.3 强大的网络请求第三方框架 Retrofit 的介绍和使用<未完待翻译>
- 使用jquery异步上传文件----SpringMVC注解开发
- 《Android开发艺术探索》读书笔记
- 复制到剪切板
- Python作为移动客户端后台服务器
- 正则表达式 linux shell
- 谷歌电子市场笔记5
- Python基础(一)之Python简介
- web端输出打印地图(ArcGIS api for Javascript)一、地图打印模板的制作
- Activity的启动模式