AjaxFileUpload.js
修改后:
(function ($, document) { $.extend({ ajaxUploadFile: function (options) { function getId() { return (new Date).getTime() } function createIFrame() { //create frame var iframeId = 'uploadIFrame' + getId(); var $iframe = $('<iframe src="javascript:false" style="display:none"></iframe>'); $iframe.attr('id', iframeId); $iframe.attr('name', iframeId); $iframe.appendTo(document.body); return $('#' + iframeId); } function createForm(actionEle, data) { //create form var newEleId = 'actionEle' + getId(); var formId = 'uploadForm' + getId(); var $form = $('<form action="#" method="POST" name="uploadForm" encoding="multipart/form-data" enctype="multipart/form-data" style="display:none"></form>'); $form.attr('id', formId); if (typeof data == "object") { for (var name in data) { $('<input type="hidden" name="' + name + '" value="' + data[name] + '" />').appendTo($form); } } var newEle = $(actionEle).clone(); $(actionEle).before(newEle); $(actionEle).attr('id', newEleId); $(actionEle).appendTo($form); $form.appendTo('body'); return $('#' + formId); } function handleError(opts, xhr, status, e) { if (opts.error) { opts.error.call(opts.context || opts, xhr, status, e); } if (opts.global) { (opts.context ? $(opts.context) : $.event).trigger("ajaxError", [xhr, opts, e]); } } function parseHttpData(r, type) { var data = !type; data = (type == "xml" || data) ? r.responseXML : r.responseText; if (type == "script") { // If the type is "script", eval it in global context $.globalEval(data) } else if (type == "json") { // Get the JavaScript object, if JSON is used. var str = $(data).text(); data = $.parseJSON(str) } else if (type == "html") { // evaluate scripts within html $("<div>").html(data).evalScripts(); } else { $("<div>").html(data) } return data; } // actionEle, url, data var opts = $.extend($.ajaxSettings, options || {}); var $iframe = createIFrame(); var $form = createForm(opts.actionEle, opts.data); // Watch for a new set of requests if (opts.global && !$.active++) { $.event.trigger("ajaxStart"); } var requestDone = false; var xml = {}; if (opts.global) $.event.trigger("ajaxSend", [xml, opts]); var uploadCallback = function (isTimeout) { var iframe = document.getElementById($iframe.attr('id')); try { if (iframe.contentWindow) { xml.responseText = iframe.contentWindow.document.body ? iframe.contentWindow.document.body.innerHTML : null; xml.responseXML = iframe.contentWindow.document.XMLDocument ? iframe.contentWindow.document.XMLDocument : iframe.contentWindow.document; } else if (iframe.contentDocument) { xml.responseText = iframe.contentDocument.document.body ? iframe.contentDocument.document.body.innerHTML : null; xml.responseXML = iframe.contentDocument.document.XMLDocument ? iframe.contentDocument.document.XMLDocument : iframe.contentDocument.document; } } catch (e) { handleError(opts, xml, null, e); } if (xml || isTimeout == "timeout") { requestDone = true; var status; try { status = isTimeout != "timeout" ? "success" : "error"; if (status != "error") { var data = parseHttpData(xml, opts.dataType); if (opts.success) opts.success(data, status); if (opts.global) $.event.trigger("ajaxSuccess", [xml, opts]); } else handleError(opts, xml, status); } catch (e) { status = "error"; handleError(opts, xml, status, e); } if (opts.global) $.event.trigger("ajaxComplete", [xml, opts]); // Handle the global AJAX counter if (opts.global && !--$.active) $.event.trigger("ajaxStop"); // Process result if (opts.complete) opts.complete(xml, status); $($iframe).unbind(); setTimeout(function () { try { $($iframe).remove(); $($form).remove(); } catch (e) { handleError(opts, xml, null, e); } }, 1000); xml = null } }; // Timeout checker if (opts.timeout > 0) { setTimeout(function () { // Check to see if the request is still happening if (!requestDone) uploadCallback("timeout"); }, opts.timeout); } try { $form.attr('action', opts.url); $form.attr('method', 'POST'); $form.attr('target', $iframe.attr('name')); $form.submit(); } catch (e) { handleError(opts, xml, null, e); } $iframe.load(uploadCallback); return {abort: function () { }}; } }) })(jQuery, document);
相关推荐
jQuery图片文件上传队列代码是一款图片和文件上传队列,支持批量上传,删除代码。
JQuery多文件上传源码 利用JQuery控制文件上传,页面中引入相关的js文件。一次选取多个文件,等待上传至服务器中指定目录。前台页面可以对已添加的文件进行删除操作,也可以多次添加文件到上传候选列表。上传文件...
jquery多文件上传
资源名:jQuery图片批量上传插件源码 资源类型:程序源代码 源码说明:实现图片预览,预览图片移除,任意张数异步上传,上传进度条指示,已选中且上传的图片不会重复上传,且不能移除 使用方法 界面顶部引入IMGUP....
jQuery多文件上传并获取大小与格式代码,可以获取文件大小,和文件格式
jquery文件上传插件(多文件选择、带进度条 ,图片预览)
JQuery大文件上传,包含c#实例,简单实用,简单易懂,超好用,超实用,直接应用到项目中,JQuery大文件上传,JQuery大文件上传
jquery多文件上传实例..............
ajax+jquery+ashx实现上传文件 简单易用,直接调用
jquery多图片上传预览 好用 jquery多图片上传预览
Jquery 多文件上传,jquery+flash 多文件上传,界面美观, 很炫的上传组件,支持中文! 本实例经过严格测试,保证能正常使用!网上其他好多实例都不能正常运行。 uploadify是一款容量小、功能强的Js批量上传工具,...
用于在网页端上传大文件,大图片,可以设置多个上传参数,提供了多种回调. 可以任意绑定id,自动生成上传表单,可以自定义文件头,其它参数,设置最大上传,最小上传,以及判断上传类型,现已支持断点续传
jquery实现图片即时上传,兼容多种浏览器,实现功能强大,代码简单,值得分析学习。
jQuery支持预览多个文件_多张图片批量上传插件jQuery支持预览多个文件_多张图片批量上传插件jQuery支持预览多个文件_多张图片批量上传插件
jquery-upload-file 基于jquery的文件上传
这是一款支持批量上传、删除图片和文件的上传插件,jQuery图片文件批量上传队列代码,注释很详细。
jquery图片jquery图片jquery图片jquery图片jquery图片jquery图片
一个以实现无刷新jquery插件实现文件上传
jquery多文件上传 demo。 jquery多文件上传。 jquery多文件上传 jquery上传