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

ajaxfileupload.js

  1. 获取文件上传url
  2. <input type="file" id="file2" name="upfile">
  3. <input type="button" value="获取文件上传url" onclick="getupfilerul();">
  1. //基本接口-获取文件上传url
  2. function getupfilerul(){
  3. $.ajaxFileUpload({
  4. url:'/lib/file/editor',
  5. fileElementId:'file2',
  6. //dataType:'json',
  7. dataType:'application/json',
  8. success:function(data,status){
  9. debugger;
  10. },
  11. error:function(data,status,e){
  12. alert(e);
  13. }
  14. });
  15. }

ajaxfileupload.js ajax上传文件(含application/json)的更多相关文章

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

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

  2. jS Ajax 上传文件报错"Uncaught TypeError: Illegal invocation"

    使用jquery ajax异步提交文件的时候报Uncaught TypeError :Illegal invocation错误,报错信息如图: 错误原因: jQuery Ajax 上传文件处理方式,使 ...

  3. 使用ajaxfileupload.js实现上传文件功能

    <div class="pictureList"> <div class="pictureItem" id="uploadItem& ...

  4. django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件

    一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...

  5. django上课笔记7-jQuery Ajax 和 原生Ajax-伪造的Ajax-三种Ajax上传文件方法-JSONP和CORS跨域资源共享

    一.jQuery Ajax 和 原生Ajax from django.conf.urls import url from django.contrib import admin from app01 ...

  6. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  7. 伪ajax上传文件

    伪ajax上传文件   最近在折腾伪ajax异步上传文件. 网上搜索了一下,发现大部分方法的input file控件都局限于form中,如果是在form外的呢? 必须动态生成一个临时form和临时if ...

  8. flask jQuery ajax 上传文件

    1.html 代码 <div> <form id="uploadForm" enctype="multipart/form-data" > ...

  9. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

随机推荐

  1. maven最全教程

    Maven 教程 1.Maven概述 Maven 是什么? Maven 是一个项目管理和整合工具.Maven 为开发者提供了一套完整的构建生命周期框架.开发团队几乎不用花多少时间就能够自动完成工程的基 ...

  2. 使用Nexus搭建Maven内部服务器

    概述         我们在使用maven时,一般通过网络上一些公共的maven仓库来获取jar包,但是有时候会碰到网速比较慢的情况就比较郁闷,Nexus是一个maven的服务器,可以让我们搭建一个本 ...

  3. ==和equals的简单比较

    前言:==和equals这个两个东西,经常放在一块做比较,下面我也给出一个简单的例子,对他俩进行一个简单的比较,先看例子,然后在看结论.(实验环境:win7+jdk7) 1:==和equals简单比较 ...

  4. windows 中安装及使用 SSH Key

    转自 简书技术博客:https://www.jianshu.com/p/a3b4f61d4747 联系管理员开通ssh功能: 重新创建环境: 下载工具包到本地机器wsCli 0.4 解压后,把相应的w ...

  5. [转]Maven - 环境配置

    Maven 是一个基于 Java 的工具,所以要做的第一件事情就是安装 JDK. 系统要求 项目 要求 JDK Maven 3.3 要求 JDK 1.7 或以上Maven 3.2 要求 JDK 1.6 ...

  6. Mac下 如何配置虚拟机软件Parallel Desktop--超详细

    Mac下 如何配置虚拟机软件Pparallel Desktop--超详细 Mac 的双系统解决方案有两种,一种是使用Boot Camp分区安装独立的Windows,一种是通过安装Parallels D ...

  7. Android Activity标签属性

    Android Activity标签属性 Activity 是 Android 系统四大应用组件之一,用户可与 Activity 提供的屏幕进行交互,以执行拨打电话.拍摄照片.发送电子邮件等操作开发者 ...

  8. Django--middleware 详解

    面对的问题: 当我们的一个网站上线后有可能遇到一些恶意的访问.比如来自对手的web爬虫:我看过一些lowB的对手,它们IP地址都不换一个的,也不 在行为上做伪装. 1.可行方法一: 在每一个view中 ...

  9. Node.js中,获取req请求的原始IP

    Node.js代码 var express = require('express'); var app = express(); var http = require('http'); var ser ...

  10. svn up 排除目录更新

    svn update --set-depth=exclude tmp 则可以排除tmp目录的更新