jquery实现异步文件上传ajaxfileupload.js
本文实例为大家分享了jquery实现异步文件上传的具体代码,供大家参考,具体内容如下
ajaxfileupload.js异步上传文件:
直接引用。
调用方法:
$.ajaxFileUpload({
url:'',//后台接口地址
type:"post",
secureuri:false,//一般设置为false
fileElementId:'ofx',//上传文件的id、name属性名
dataType:'json',
success:function(data,status){
},
error:function(data,status,e){
}
});
下面是ajaxfileupload.js,可直接复制并引用。
jQuery.extend({
createUploadIframe:function(id,uri)
{
//createframe
varframeId='jUploadFrame'+id;
variframeHtml=' ';
jQuery(iframeHtml).appendTo(document.body);
returnjQuery('#'+frameId).get(0);
},
createUploadForm:function(id,fileElementId,data,fileElement)
{
//createform
varformId='jUploadForm'+id;
varfileId='jUploadFile'+id;
varform=jQuery('');
if(data)
{
for(variindata)
{
jQuery(' ').appendTo(form);
}
}
if(typeof(fileElementId)=='string'){
fileElementId=[fileElementId];
}
for(variinfileElementId){
varoldElement=jQuery('#'+fileElementId[i]);
varnewElement=jQuery(oldElement).clone();
jQuery(oldElement).attr('id',fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
}
//setattributes
jQuery(form).css('position','absolute');
jQuery(form).css('top','-1200px');
jQuery(form).css('left','-1200px');
jQuery(form).appendTo('body');
returnform;
},
ajaxFileUpload:function(s){
//TODOintroduceglobalsettings,allowingtheclienttomodifythemforallrequests,notonlytimeout
s=jQuery.extend({},jQuery.ajaxSettings,s);
varid=newDate().getTime()
varform=jQuery.createUploadForm(id,s.fileElementId,(typeof(s.data)=='undefined'?false:s.data),s.fileElement);
vario=jQuery.createUploadIframe(id,s.secureuri);
varframeId='jUploadFrame'+id;
varformId='jUploadForm'+id;
//Watchforanewsetofrequests
if(s.global&&!jQuery.active++)
{
jQuery.event.trigger("ajaxStart");
}
varrequestDone=false;
//Createtherequestobject
varxml={}
if(s.global)
jQuery.event.trigger("ajaxSend",[xml,s]);
//Waitforaresponsetocomeback
varuploadCallback=function(isTimeout)
{
vario=document.getElementById(frameId);
try
{
if(io.contentWindow)
{
xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
}elseif(io.contentDocument)
{
xml.responseText=io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
xml.responseXML=io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}
}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
if(xml||isTimeout=="timeout")
{
requestDone=true;
varstatus;
try{
status=isTimeout!="timeout"?"success":"error";
//Makesurethattherequestwassuccessfulornotmodified
if(status!="error")
{
//processthedata(runsthexmlthroughhttpDataregardlessofcallback)
vardata=jQuery.uploadHttpData(xml,s.dataType);
//Ifalocalcallbackwasspecified,fireitandpassitthedata
if(s.success)
s.success(data,status);
//Firetheglobalcallback
if(s.global)
jQuery.event.trigger("ajaxSuccess",[xml,s]);
}else
jQuery.handleError(s,xml,status);
}catch(e)
{
status="error";
jQuery.handleError(s,xml,status,e);
}
//Therequestwascompleted
if(s.global)
jQuery.event.trigger("ajaxComplete",[xml,s]);
//HandletheglobalAJAXcounter
if(s.global&&!--jQuery.active)
jQuery.event.trigger("ajaxStop");
//Processresult
if(s.complete)
s.complete(xml,status);
jQuery(io).unbind()
setTimeout(function()
{try
{
jQuery(io).remove();
jQuery(form).remove();
}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
},100)
xml=null
}
}
//Timeoutchecker
if(s.timeout>0)
{
setTimeout(function(){
//Checktoseeiftherequestisstillhappening
if(!requestDone)uploadCallback("timeout");
},s.timeout);
}
try
{
varform=jQuery('#'+formId);
jQuery(form).attr('action',s.url);
jQuery(form).attr('method','POST');
jQuery(form).attr('target',frameId);
if(form.encoding)
{
jQuery(form).attr('encoding','multipart/form-data');
}
else
{
jQuery(form).attr('enctype','multipart/form-data');
}
jQuery(form).submit();
}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
jQuery('#'+frameId).load(uploadCallback);
return{abort:function(){
try
{
jQuery('#'+frameId).remove();
jQuery(form).remove();
}
catch(e){}
}};
},
uploadHttpData:function(r,type){
vardata=!type;
data=type=="xml"||data?r.responseXML:r.responseText;
//Ifthetypeis"script",evalitinglobalcontext
if(type=="script")
jQuery.globalEval(data);
//GettheJavaScriptobject,ifJSONisused.
if(type=="json"){
以下为新增代码///
data=r.responseText;
varstart=data.indexOf(">");
if(start!=-1){
varend=data.indexOf("<",start+1);
if(end!=-1){
data=data.substring(start+1,end);
}
}
}
///以上为新增代码///
eval("data="+data);
//evaluatescriptswithinhtml
if(type=="html")
jQuery("").html(data).evalScripts();
returndata;
},
handleError:function(s,xml,status,e){
//Ifalocalcallbackwasspecified,fireit
if(s.error)
s.error(xml,status,e);
//Firetheglobalcallback
if(s.global)
jQuery.event.trigger("ajaxError",[xml,s,e]);
}
});
更多精彩内容,请点击《jQuery上传操作汇总》,《ajax上传技术汇总》进行深入学习和研究。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。