Created
October 14, 2015 05:39
-
-
Save pfmiles/4ac5ac27c5ab5ed3c32c to your computer and use it in GitHub Desktop.
js异步上传文件, 可绑定的控件事件函数生成函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* created by pf_miles, 2015-10-13 | |
*/ | |
/** | |
* 根据参数,返回一个执行表单异步上传文件的函数, 该函数可以被绑定到页面控件的事件上使用 | |
* @param {String} fileFormId 要提交的formId | |
* @param {String} formAction 要提交的formAction | |
* @param {Function} successHandler 上传成功处理函数, 接受一个json参数为server端返回信息 | |
* @param {Function} failHandler 上传失败处理函数, 接受一个json参数为server端返回信息 | |
*/ | |
function genAsyncUploadFunction(fileFormId, formAction, successHandler, failHandler){ | |
return function () { | |
var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none"></iframe>'); | |
$("body").append(iframe); | |
var form = $('#' + fileFormId); | |
form.attr("action", formAction); | |
form.attr("method", "post"); | |
form.attr("encoding", "multipart/form-data"); | |
form.attr("enctype", "multipart/form-data"); | |
form.attr("target", "postiframe"); | |
form.submit(); | |
$("#postiframe").load(function () { | |
iframeContents = this.contentWindow.document.body.innerText; | |
var rst = JSON.parse(iframeContents); | |
if(rst.success){ | |
successHandler(rst); | |
}else{ | |
failHandler(rst); | |
} | |
}); | |
return false; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment