一、问题
你在网上下载的ajaxfileuploa.js在使用的时候可能会给你报错误:
Uncaught TypeError: Object function (a,b){return new e.fn.init(a,b,h)} has no method ‘handleError'
这是因为高版本的jQuery中取消了handleError方法,在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] ); } },
二、参数说明
url 上传处理程序地址。
fileElementId 需要上传的文件域的ID,即的ID。
secureuri 是否启用安全提交,默认为false。
dataType 服务器返回的数据类型。可以为xml,script,json,html。如果不填写,jQuery会自动判断。
success 提交成功后自动执行的处理函数,参数data就是服务器返回的数据。
error 提交失败自动执行的处理函数。
data 自定义参数。这个东西比较有用,当有数据是与上传的图片相关的时候,这个东西就要用到了。
type 当要提交自定义参数时,这个参数要设置成post
三、使用
$.ajaxFileUpload({ type: 'post', url: siteUrl + '/upload/doUpload', secureuri: false, //是否需要安全协议,一般设置为false fileElementId: 'txt_file', //文件上传域的ID data: { 230, height: 200, '<?php echo $csrf["name"];?>': '<?php echo $csrf["hash"];?>' }, dataType: 'text', success: function (data) { data = JSON.parse(data); console.log(data); if (data.code == 1) { layer.msg("上传文件失败", {icon: 2}) } else if (data.code == 0) { layer.msg("上传文件成功", {icon: 1}); $("#thumb").val(data.path); } } });