jquery.upload-1.0.2.js
2017-08-16 00:00
78 查看
/* * jQuery.upload v1.0.2 * * Copyright (c) 2010 lagos * Dual licensed under the MIT and GPL licenses. * * http://lagoscript.org */ (function($) { var uuid = 0; $.fn.upload = function(url, data, callback, type) { var self = this, inputs, checkbox, checked, iframeName = 'jquery_upload' + ++uuid, iframe = $('<iframe name="' + iframeName + '" style="position:absolute;top:-9999px;height:0px;width:0px;" />').appendTo('body'), form = '<form target="' + iframeName + '" method="post" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px;display:inline " enctype="multipart/form-data" />'; if ($.isFunction(data)) { type = callback; callback = data; data = {}; } checkbox = $('input:checkbox', this); checked = $('input:checked', this); form = self.wrapAll(form).parent('form').attr('action', url); // Make sure radios and checkboxes keep original values // (IE resets checkd attributes when appending) checkbox.removeAttr('checked'); checked.attr('checked', true); inputs = createInputs(data); inputs = inputs ? $(inputs).appendTo(form) : null; form.submit(function() { iframe.load(function() { var data = handleData(this, type), checked = $('input:checked', self); form.after(self).remove(); checkbox.removeAttr('checked'); checked.attr('checked', true); if (inputs) { inputs.remove(); } setTimeout(function() { iframe.remove(); if (type === 'script') { $.globalEval(data); } if (callback) { callback.call(self, data); } }, 0); }); }).submit(); return this; }; function createInputs(data) { return $.map(param(data), function(param) { return '<input type="hidden" name="' + param.name + '" value="' + param.value + '"/>'; }).join(''); } function param(data) { if ($.isArray(data)) { return data; } var params = []; function add(name, value) { params.push({name:name, value:value}); } if (typeof data === 'object') { $.each(data, function(name) { if ($.isArray(this)) { $.each(this, function() { add(name, this); }); } else { add(name, $.isFunction(this) ? this() : this); } }); } else if (typeof data === 'string') { $.each(data.split('&'), function() { var param = $.map(this.split('='), function(v) { return decodeURIComponent(v.replace(/\+/g, ' ')); }); add(param[0], param[1]); }); } return params; } function handleData(iframe, type) { var data, contents = $(iframe).contents().get(0); if ($.isXMLDoc(contents) || contents.XMLDocument) { return contents.XMLDocument || contents; } data = $(contents).find('body').html(); try{ switch (type) { case 'xml': data = parseXml(data); break; case 'json': if(data.indexOf("pre")>0){ data=jQuery.parseJSON(jQuery(data).text()); }else{ eval("data = " + data); } //data = window.eval('(' + data + ')'); break; } }catch(e){ return '服务器异常'; } return data; } function parseXml(text) { if (window.DOMParser) { return new DOMParser().parseFromString(text, 'application/xml'); } else { var xml = new ActiveXObject('Microsoft.XMLDOM'); xml.async = false; xml.loadXML(text); return xml; } } })(jQuery);
相关文章推荐
- Jquery+ajaxfileupload上传文件
- HTML5:多文件上传 Upload multiple files at once with HTML5, jQuery and PHP
- JQuery File Upload 插件 出现 “empty file upload result” 错误的解决方案。
- asp.net配置web.config支持jQuery.Uploadify插件上传大文件
- jquery.uploadify插件实现图片上传和预览效果
- jQuery Uploadify上传插件使用中的问题 中文字符文件名转英文
- jquery uploadify文件上传插件用法精析
- jQuery插件之ajaxFileUpload
- 【转】In ASP.NET using jQuery Uploadify upload attachment
- jQuery插件之ajaxFileUpload
- 使用jquery.uploadify、jquery.Jcrop和php实现炫酷的图片裁剪
- jquery uploadify隐藏上传进度的实现方法
- jQuery uploadify在谷歌和火狐浏览器上传失败的解决方案
- jquery ajaxfileupload异步上传插件使用详解
- jquery uploadfiy文件资料
- jQuery插件之ajaxFileUpload(异步上传图片并实时显示,并解决onchange后ajaxFileUpload失效问题)
- Jquery.Uploadify 上传文件插件的使用
- jQuery之upload2
- jQuery插件之ajaxFileUpload上传文件
- jquery之ajaxfileupload异步上传插件