您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于ajaxfileupload的使用方法以及一些问题

2024/6/28 19:14:49发布27次查看
使用问题:
1.ajax-fileupload.js handleerror 异常 由于本来handleerror方法是jquery的方法,但jquery到了某个版本这个方法就去掉了没有了
所以最简单有效的方式就是在ajaxfileupload.js中添加上该方法, 方法如下:
handleerror: function (s, xhr, status, e) { // if a local callback was specified, fire it if (s.error) { s.error.call(s.context || s, xhr, status, e); } // fire the global callback if (s.global) { (s.context ? jquery(s.context) : jquery.event).trigger("ajaxerror", [xhr, s, e]); } },
2.文件域 绑定change事件,实现每次选择图片就上传到服务器,并返回图片路径,让浏览器显示图片 但触发一次change事件后,下次就不会再触发change事件
原因:由于ajaxfileupload把原来的file元素替换成新的file元素,所以之前绑定的change事件就失效了, 解决方法:在 $.ajaxfileupload({option})中的回调函数里 重新绑定change事件。
使用demo
视图页:
//绑定事件 $("#文件上传域的id").change(function () { uploadimg(); }); uploadimg = function() { //判断内容是否为空 if ($("#文件上传域的id").val().length <= 0) { return; }; //执行异步上传 $.ajaxfileupload({ url: '@url.action("uploadheadphoto","userinfo")', //用于文件上传的服务器端请求地址 type: 'post', data: { id: $("#userid").val() },//自定义参数 secureuri: false, //是否需要安全协议,一般设置为false fileelementid: '文件上传域的id', //文件上传域的id datatype: 'json', //返回值类型 一般设置为json success: function(data) //服务器成功响应处理函数 { //由于ajaxfileupload把原来的file元素替换成新的file元素,所以之前绑定的change事件就失效了,需要重新绑定一下 $("#文件上传域的id").change(function () { uploadimg(); }); //do something.... } }); };
控制器: 由于前端要求返回的是json格式的数据,所以这里要返回json格式数据, 但ajaxfileupload,需要的json数据是字符串 所以 return json(). 这里不能使用 ,需要使用newtonsoft.json里的方法序列化成json格式的字符串
public actionresult uploadheadphoto(httppostedfilebase uheadphoto,int id) { if (uheadphoto == null) { return content(jsonconvert.serializeobject(new { status = "no", msg = "上传头像不能为空" })); } /* 判断文件格式代码省略.....*/ uheadphoto.saveas(request.mappath("/headphoto/1.jpg")); }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product