文件$.ajaxFileUpload的实现(Spring MVC架构)
来源:互联网 发布:淘宝扶植版免费模板 编辑:程序博客网 时间:2024/05/16 05:27
前端时间,在做项目的时候,遇见了注册的时候有需要上传图片的情况。因为注册的时候,用户信息还没有生成,所以希望和注册信息一起提交。
网上也有许多$.ajaxFileUpload的说明,下面是自己实现的具体代码,希望能帮上各位。
前提:
引入jquery的js与文件ajax的js,注意2个的版本问题。
jquery-1.8.3.min.js
ajaxfileupload.js
为了支持多个图片的上传,ajaxfileupload.js做了一些修正如下。
createUploadForm: function(id, fileElementId, data, fileElementTypeFlag){//create formvar formId = 'jUploadForm' + id;var fileId = 'jUploadFile' + id;var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');if(data){for(var i in data){jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);}}if(fileElementTypeFlag == 'id'){//var oldElement = jQuery('#' + fileElementId);//var newElement = jQuery(oldElement).clone();//jQuery(oldElement).attr('id', fileId);//jQuery(oldElement).before(newElement);//jQuery(oldElement).appendTo(form);if (typeof (fileElementId) == 'string') { fileElementId = [fileElementId]; }for(var i in fileElementId){ var oldElement = jQuery('#' + fileElementId[i]); var newElement = jQuery(oldElement).clone(); jQuery(oldElement).attr('id', fileId); jQuery(oldElement).before(newElement); jQuery(oldElement).appendTo(form); } }else{var oldElement = jQuery('.'+fileElementId);var newElement = jQuery(oldElement).clone();jQuery(oldElement).appendTo(form);}//set attributesjQuery(form).css('position', 'absolute');jQuery(form).css('top', '-1200px');jQuery(form).css('left', '-1200px');jQuery(form).appendTo('body');return form; },
下面是前台与后台的代码,先执行文件的上传,上传成功后,返回上传的文件的保存路径,然后返回给前台,与注册的信息
一起插入数据库中。
$.ajaxFileUpload({ url:'../../../../login_photoUpload', //需要链接到服务器地址 secureuri:false, fileElementId:['photocardpath1','photocardpath2'], //文件选择框的id属性 dataType: 'json', //服务器返回的格式,可以是json success: function(data){if("success" == data.result){var code = loginname+",fh,"+password+",fh,"+email+",fh,"+realname+",fh,"+phonenumber+",fh,"+data.photocardpath1+",fh,"+data.photocardpath2;$.ajax({type: "POST",url: '../../../../login_regist', data: {KEYDATA:code,tm:new Date().getTime()},dataType:'json',async: false, cache: false,success: function(data){if("success" == data.result){ $('#myModal2_2').modal('show');}else if("loginNameErr" == data.result){$("#registUsername").tips({side : 1,msg : "用户名重复",bg : '#FF5080',time : 10});$("#registUsername").focus();}else{$("#registUsername").tips({side : 1,msg : data.result,bg : '#FF5080',time : 10});$("#registUsername").focus();}}});}else{alert("证件上传失败,失败原因: " +data.result);}}, error: function (data, status, e) { alert("证件上传失败,注册失败!") } }
java部分(SPRING+MVC)
@RequestMapping(value="/login_photoUpload")@ResponseBodypublic Object login_photoUpload(@RequestParam(value="photocardpath1",required=false) MultipartFile photocardpath1,@RequestParam(value="photocardpath2",required=false) MultipartFile photocardpath2)throws Exception{String errInfo = "";Map<String,String> map = new HashMap<String,String>();String ffile = DateUtil.getDays(), fileName1 = "",fileName2 = "";try {if (null != photocardpath1 && !photocardpath1.isEmpty()) {String filePath = PathUtil.getClasspath() + Const.FILEPATHIMG + ffile;//文件上传路径fileName1 = FileUpload.fileUp(photocardpath1, filePath, this.get32UUID());//执行上传正面身份证fileName2 = FileUpload.fileUp(photocardpath2, filePath, this.get32UUID());//执行上传反面身份证}errInfo = "success";}catch (Exception e) {errInfo = "注册上传图片失败失败,errmsg="+ e.getMessage();}map.put("result", errInfo);map.put("photocardpath1", ffile+ File.separator +fileName1);map.put("photocardpath2", ffile+ File.separator +fileName2);return AppUtil.returnObject(new PageData(), map);}
1 0
- 文件$.ajaxFileUpload的实现(Spring MVC架构)
- Spring MVC+ajaxfileupload实现文件上传下载
- spring mvc中ajaxfileupload多文件上传
- spring mvc+ajaxfileupload 实现异步上传图片
- spring mvc 文件、图片上传(极简)ajaxFileUpload 出现 syntax error 的错误
- ajaxfileupload文件上传返回值处理 ajaxfileupload.js + spring mvc文件上传
- spring mvc 文件、图片上传(极简)ajaxFileUpload
- spring-mvc使用ajaxFileUpload上传文件总是进入error方法
- AjaxFileUpload实现文件上传
- spring 4mvc下载文件的实现
- spring MVC 文件上传下载的实现
- spring mvc 下使用ajaxfileupload.js 异步上传文件 并返回信息 各种问题解决
- Spring MVC 实现文件上传
- spring MVC实现文件上传
- Spring MVC实现文件上传
- Spring mvc实现文件上传下载
- Spring mvc实现文件上传
- Spring MVC实现文件上传下载
- Package Control Messages
- Buffer
- 兼容storm(beta版)
- SpringMVC文件下载
- WEB下使用的OFFICE控件介绍
- 文件$.ajaxFileUpload的实现(Spring MVC架构)
- bomb
- Python中sorted()方法的用法
- NSPredicate 利用谓词过滤做判断
- 解决Jsp报错:Multiple annotations found at this line
- 数据结构 — 图之邻接表存储创建和深度优先遍历
- 早该知道的7个JavaScript技巧
- 度量快速开发平台ExportToExcel使用介绍
- 关于切面编程的xml配置