1. jQuery.extend({
  2. createUploadIframe: function(id, uri)
  3. {
  4. //create frame
  5. var frameId = 'jUploadFrame' + id;
  6.  
  7. if(window.ActiveXObject) {
  8. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
  9. if(typeof uri== 'boolean'){
  10. io.src = 'javascript:false';
  11. }
  12. else if(typeof uri== 'string'){
  13. io.src = uri;
  14. }
  15. }
  16. else {
  17. var io = document.createElement('iframe');
  18. io.id = frameId;
  19. io.name = frameId;
  20. }
  21. io.style.position = 'absolute';
  22. io.style.top = '-1000px';
  23. io.style.left = '-1000px';
  24.  
  25. document.body.appendChild(io);
  26.  
  27. return io
  28. },
  29. createUploadForm: function(id, fileElementId)
  30. {
  31. //create form
  32. var formId = 'jUploadForm' + id;
  33. var fileId = 'jUploadFile' + id;
  34. var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  35. var oldElement = $('#' + fileElementId);
  36. var newElement = $(oldElement).clone();
  37. $(oldElement).attr('id', fileId);
  38. $(oldElement).before(newElement);
  39. $(oldElement).appendTo(form);
  40. //set attributes
  41. $(form).css('position', 'absolute');
  42. $(form).css('top', '-1200px');
  43. $(form).css('left', '-1200px');
  44. $(form).appendTo('body');
  45. return form;
  46. },
  47.  
  48. ajaxFileUpload: function(s) {
  49. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  50. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  51. var id = s.fileElementId;
  52. var form = jQuery.createUploadForm(id, s.fileElementId);
  53. var io = jQuery.createUploadIframe(id, s.secureuri);
  54. var frameId = 'jUploadFrame' + id;
  55. var formId = 'jUploadForm' + id;
  56. // Watch for a new set of requests
  57. if ( s.global && ! jQuery.active++ )
  58. {
  59. jQuery.event.trigger( "ajaxStart" );
  60. }
  61. var requestDone = false;
  62. // Create the request object
  63. var xml = {}
  64. if ( s.global )
  65. jQuery.event.trigger("ajaxSend", [xml, s]);
  66. // Wait for a response to come back
  67. var uploadCallback = function(isTimeout)
  68. {
  69. var io = document.getElementById(frameId);
  70. try
  71. {
  72. if(io.contentWindow)
  73. {
  74. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  75. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  76.  
  77. }else if(io.contentDocument)
  78. {
  79. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  80. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  81. }
  82. }catch(e)
  83. {
  84. jQuery.handleError(s, xml, null, e);
  85. }
  86. if ( xml || isTimeout == "timeout")
  87. {
  88. requestDone = true;
  89. var status;
  90. try {
  91. status = isTimeout != "timeout" ? "success" : "error";
  92. // Make sure that the request was successful or notmodified
  93. if ( status != "error" )
  94. {
  95. // process the data (runs the xml through httpData regardless of callback)
  96. var data = jQuery.uploadHttpData( xml, s.dataType );
  97. // If a local callback was specified, fire it and pass it the data
  98. if ( s.success )
  99. s.success( data, status );
  100.  
  101. // Fire the global callback
  102. if( s.global )
  103. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  104. } else
  105. jQuery.handleError(s, xml, status);
  106. } catch(e)
  107. {
  108. status = "error";
  109. jQuery.handleError(s, xml, status, e);
  110. }
  111.  
  112. // The request was completed
  113. if( s.global )
  114. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  115.  
  116. // Handle the global AJAX counter
  117. if ( s.global && ! --jQuery.active )
  118. jQuery.event.trigger( "ajaxStop" );
  119.  
  120. // Process result
  121. if ( s.complete )
  122. s.complete(xml, status);
  123.  
  124. jQuery(io).unbind()
  125.  
  126. setTimeout(function()
  127. { try
  128. {
  129. $(io).remove();
  130. $(form).remove();
  131.  
  132. } catch(e)
  133. {
  134. jQuery.handleError(s, xml, null, e);
  135. }
  136.  
  137. }, )
  138.  
  139. xml = null
  140.  
  141. }
  142. }
  143. // Timeout checker
  144. )
  145. {
  146. setTimeout(function(){
  147. // Check to see if the request is still happening
  148. if( !requestDone ) uploadCallback( "timeout" );
  149. }, s.timeout);
  150. }
  151. try
  152. {
  153. // var io = $('#' + frameId);
  154. var form = $('#' + formId);
  155. $(form).attr('action', s.url);
  156. $(form).attr('method', 'POST');
  157. $(form).attr('target', frameId);
  158. if(form.encoding)
  159. {
  160. form.encoding = 'multipart/form-data';
  161. }
  162. else
  163. {
  164. form.enctype = 'multipart/form-data';
  165. }
  166. $(form).submit();
  167.  
  168. } catch(e)
  169. {
  170. jQuery.handleError(s, xml, null, e);
  171. }
  172. if(window.attachEvent){
  173. document.getElementById(frameId).attachEvent('onload', uploadCallback);
  174. }
  175. else{
  176. document.getElementById(frameId).addEventListener('load', uploadCallback, false);
  177. }
  178. return {abort: function () {}};
  179.  
  180. },
  181.  
  182. uploadHttpData: function( r, type ) {
  183. var data = !type;
  184. data = type == "xml" || data ? r.responseXML : r.responseText;
  185. // If the type is "script", eval it in global context
  186. if ( type == "script" )
  187. jQuery.globalEval( data );
  188. // Get the JavaScript object, if JSON is used.
  189. if ( type == "json" )
  190. eval( "data = " + data );
  191. // evaluate scripts within html
  192. if ( type == "html" )
  193. jQuery("<div>").html(data).evalScripts();
  194. //alert($('param', data).each(function(){alert($(this).attr('value'));}));
  195. return data;
  196. }
  197. })

ajaxfileupload.js的更多相关文章

  1. 使用ajaxfileupload.js实现文件上传

    ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 1.url  上传处理程序地址. 2,file ...

  2. ajaxfileupload.js的简单使用

    上传文件 未选择任何文件 引入 <script src="../javaScript/ajaxfileupload.js"></script> <bu ...

  3. 利用ajaxfileupload.js异步上传文件

    1.引入ajaxfileupload.js 2.html代码 <input type="file" id="enclosure" name="e ...

  4. jquery.ajaxfileupload.js

    jquery.ajaxfileupload.js上传插件,利用iframe提交不刷新页面功能完成. /* // jQuery Ajax File Uploader // // @author: Jor ...

  5. java jfinal + ajaxfileupload.js 上传

    功能上传 需求:同时上传多张图片 前端:jquery.ajaxfileupload.js 后端:jfinal upload.htm <html> <body> <div ...

  6. 文件上传ajaxfileupload.js插件

    Html:  <div class="container">         <form id="form" runat="serv ...

  7. SpringMVC结合ajaxfileupload.js实现文件无刷新上传

    直接看代码吧,注释都在里面 首先是web.xml <?xml version="1.0" encoding="UTF-8"?> <web-ap ...

  8. 通过修改ajaxFileUpload.js实现多图片动态上传并实现预览

    参考:http://smotive.iteye.com/blog/1903606 大部分我也是根据他的方法修改的,我也要根据name实现动态的多文件上传功能,但是有个问题使我一直无法实现多文件上传. ...

  9. 关于ajaxfileupload.js一些问题和上传图片就立即显示图片功能

    ajaxfileupload.js是上传文件的一个插件,最近碰到的一个问题是在谷歌浏览器上传文件之后,原文本框中的文件名称消失,网上搜了好长时间也没有十分满意的答案.无刷新上传文件我想到的只有ajax ...

  10. 引用(ajaxfileupload.js) ajaxfileupload.js报这错jQuery.handleError is not a function

    jQuery.handleError is not a function 原因是,经测试handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.6 和1.7中都没有这个 ...

随机推荐

  1. loadrunner资源过滤器

    通过该功能可以实现排除某个资源,很实用 Download Filters功能 帮助在回放脚本的时候对某些特定的访问进行屏蔽,解决页面读取中跨服务器带来数据影响的问题. 过滤规则中有3中策略,即URL. ...

  2. background为圆角的表框,dp转Px,Px转dp

    圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...

  3. 读懂Android项目结构目录

    我们看到下图:当我们创建了第一Android项目的时候有没有被吓到.怎么这么多目录,好头晕啊!没事, 那我们今天就了解一下这些目录是做什么的: src: src 目录是放置我们所有 Java 代码的地 ...

  4. Ajax实现点击省份显示相应城市

    功能:不用级联效果,自己写ajax,从接口读取省份城市数据,实现点击省份显示相应城市.后端根据省份ID,给前端返回城市. 一.DOM结构(套用blade模板) <div class=" ...

  5. jQuery操作radiobutton

    1.获取某个radio选中的值,有三种方法 $("input:radio:checked").val()(*我最喜欢)  ; $("input[type='radio'] ...

  6. Java第一天:安装搭建Java开发环境

    Java是面向对象的语言.它是通过虚拟机的运行机制来实现“跨平台”的. 这里不多说其他的,进入正题先,学习任何语言前的第一步都是要先搭建好开发环境,Java开发环境搭建如下: 1.到官网 http:/ ...

  7. Codeforces 567C Geometric Progression(思路)

    题目大概说给一个整数序列,问里面有几个包含三个数字的子序列ai,aj,ak,满足ai*k*k=aj*k=ak. 感觉很多种做法的样子,我想到这么一种: 枚举中间的aj,看它左边有多少个aj/k右边有多 ...

  8. UIImage两种初始化的区别

    UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ...

  9. Teamwork[HDU4494]

    Teamwork Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...

  10. js 四舍五入保留二位小数

    1. 最笨的办法....... [我就怎么干的.........] function get() { var s = 22.127456 + ""; var str = s.sub ...