适用于jquery1.11.1的ajaxfileupload.js

ajaxfileupload源码

解决上传成功不走success的问题

解决高版本jquery兼容性问题

jQuery.extend({

createUploadIframe: function(id, uri)
{
//create frame
var frameId = ‘jUploadFrame‘ + id;
var iframeHtml = ‘<iframe id="‘ + frameId + ‘" name="‘ + frameId + ‘" style="position:absolute; top:-9999px; left:-9999px"‘;
if(window.ActiveXObject)
{
if(typeof uri== ‘boolean‘){
iframeHtml += ‘ src="‘ + ‘javascript:false‘ + ‘"‘;

}
else if(typeof uri== ‘string‘){
iframeHtml += ‘ src="‘ + uri + ‘"‘;

}
}
iframeHtml += ‘ />‘;
jQuery(iframeHtml).appendTo(document.body);

return jQuery(‘#‘ + frameId).get(0);
},
createUploadForm: function(id, fileElementId, data)
{
//create form
var 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);
}
}
var oldElement = jQuery(‘#‘ + fileElementId);
var newElement = oldElement.clone(true);
newElement[0].files = oldElement[0].files;
oldElement.attr(‘id‘, fileId);
oldElement.before(newElement);
oldElement.appendTo(form);

//set attributes
jQuery(form).css(‘position‘, ‘absolute‘);
jQuery(form).css(‘top‘, ‘-1200px‘);
jQuery(form).css(‘left‘, ‘-1200px‘);
jQuery(form).appendTo(‘body‘);
return form;
},

ajaxFileUpload: function(s) {
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
s = jQuery.extend({}, jQuery.ajaxSettings, s);
var id = new Date().getTime()
var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)==‘undefined‘?false:s.data));
var io = jQuery.createUploadIframe(id, s.secureuri);
var frameId = ‘jUploadFrame‘ + id;
var formId = ‘jUploadForm‘ + id;
// Watch for a new set of requests
if ( s.global && ! jQuery.active++ )
{
jQuery.event.trigger( "ajaxStart" );
}
var requestDone = false;
// Create the request object
var xml = {}
if ( s.global )
jQuery.event.trigger("ajaxSend", [xml, s]);
// Wait for a response to come back
var uploadCallback = function(isTimeout)
{
var io = 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;

}else if(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;
var status;
try {
status = isTimeout != "timeout" ? "success" : "error";
// Make sure that the request was successful or notmodified
if ( status != "error" )
{
// process the data (runs the xml through httpData regardless of callback)
var data = jQuery.uploadHttpData( xml, s.dataType );
// If a local callback was specified, fire it and pass it the data
if ( s.success )
s.success( data, status );

// Fire the global callback
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);
}

// The request was completed
if( s.global )
jQuery.event.trigger( "ajaxComplete", [xml, s] );

// Handle the global AJAX counter
if ( s.global && ! --jQuery.active )
jQuery.event.trigger( "ajaxStop" );

// Process result
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

}
}
// Timeout checker
if ( s.timeout > 0 )
{
setTimeout(function(){
// Check to see if the request is still happening
if( !requestDone ) uploadCallback( "timeout" );
}, s.timeout);
}
try
{

var form = 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 () {}};

},

uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
data = data.replace("<pre>", "").replace("</pre>", "");
// evaluate scripts within html
if ( type == "html" )
jQuery("<div>").html(data).evalScripts();

return data;
}
})

附html代码和服务端代码

<input type="file" id="importFile" name="importFile" accept="text/plain"  style="display: none" />

#region 上传文件 读取导入的内容
public JsonResult Upload()
{
HttpFileCollectionBase hfc = Request.Files;
string Path = "";
List<LgtdAndLttd> list = new List<LgtdAndLttd>();
string PhysicalPath = "";
if (hfc.Count > 0)
{
if (hfc[0].FileName.Contains("\\"))
{
int index = hfc[0].FileName.LastIndexOf(‘\\‘);
Path = "/Upload" +hfc[0].FileName.Substring(index, hfc[0].FileName.Length - index);
}
else
Path = "/Upload/" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + System.IO.Path.GetFileName(hfc[0].FileName);
PhysicalPath = Server.MapPath(Path);
hfc[0].SaveAs(PhysicalPath);
StreamReader sr = new StreamReader(PhysicalPath);
LgtdAndLttd sta = new LgtdAndLttd();
while(sr.Peek()>=0)
{
sta.LGTD = sr.ReadLine().Split(‘|‘)[0];
sta.LTTD = sr.ReadLine().Split(‘|‘)[1];
list.Add(sta);
}
}
return Json(list);
}
#endregion

时间: 2024-11-06 07:09:16

适用于jquery1.11.1的ajaxfileupload.js的相关文章

2016/04/18 ①注册 注册处理 ② 审核 审核处理 ③登录 登录处理 ④需要jquery-1.11.2.min.js DBDA.php

① 注册   zhuceye.php 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script src="jquery-1.11.2.min.js"></script> 7 </head>

解决:jquery-1.11.1.min.js红叉问题

工程中导入jquery-1.11.1.min.js,工程正常运行.但是jquery-1.11.1.min.js一直显示红叉. 解决方法如下: 红叉的原因是:myeclipse没有去验证它! 选中js文件,右键Myeclipse--ManaValidation--ExcludeResource--(选中全部或者那个js)

ajaxfileupload.js实现文件上传

1 <head runat="server"> 2 <title></title> 3 <script src="Scripts/jquery-1.3.1.min.js" type="text/javascript"></script> 4 <script src="Scripts/ajaxfileupload.js" type="text/javas

ajaxfileupload.js异步提交图片

碰到问题的解决方法:http://blog.yadgen.com/?p=970 <script type="text/javascript" src="_LIB_/jquery-1.9.0.min.js"></script> <script type="text/javascript" src="_JS_/jquery.ajaxfileupload.js"></script> $

jquery1.11 操作checkbox:全选、取消全选、获取选择元素、获取取消选择元素(总结)

jquery1.11.1版本完成对checkbox的操作 1. 使用属性prop设置选中状态 2.使用:checked和:not(:checked)获取选中的元素 源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1&quo

我的jQuery源码读后感-jquery1.11.x 集成了 AMD

jquery1.11.x 集成了 AMD AMD 加入到了 jQuery, jQuery 把源码切分成各个逻辑模块. ready 整合成依赖 Deferred 的模块. 有些模块被切分成一些更加利于维护的片段. var 文件夹指定的模块只包含共享的变量声明. (function( global, factory ) { if( typeof module === "object" && typeof module.exports === "object&quo

利用ajaxfileupload.js异步上传文件

1.引入ajaxfileupload.js 2.html代码 <input type="file" id="enclosure" name="enclosure"> <button id="upClick" >上传</button> 注意这里的input控件的id和name必须一致:这样在后台利用springMVC接受文件的时候能对应起来: 3.JS代码 <script type=&q

ajaxfileupload.js插件结合一般处理文件实现Ajax无刷新上传

先上几张图更直观展示一下要实现的功能.本功能主要通过Jquery ajaxfileupload.js插件结合ajaxUpFile.ashx一般应用程序处理文件实现Ajax无刷新上传功能,结合NPOI2.0实现数据读取.这个功能在实际工作种经经常使用到,希望能给须要做这方面的人有些帮助. 一.功能页面布局及介绍 1.上传页面布局及input file上传功能 2.上传页面文件正在上传效果 3.上传完毕效果,多文件展示区 二.功能代码实现及资源引用 1.js资源文件引用 html页面js引用.须要引

ajax图片上传(ajaxfileupload.js插件)

向后台交互方式: 1.form表单提交,action="url" 2.ajax异步提交 区别: form在提交之后无法获得后台的回调参数,只能由后台工作人员控制提交成功之后的路径跳转:优点是可以直接提交文件,如:图片.txt文件等: ajax很好的处理字符格式的提交并获取提交成功之后的回调参数,但是无法提交图片. ajax提交图片只能把图片格式转化为进制流模式 解决方式: 1 <script type="text/javascript" src="j