`
zhaixp1949
  • 浏览: 7591 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

AjaxFileUpload.js

 
阅读更多


jQuery.extend({
 

    createUploadIframe: function(id, uri)
 {
  //create frame
  var frameId = 'jUploadFrame' + id;
//  if(window.ActiveXObject) {
//   if(jQuery.browser.version=="9.0"){
//    var io = document.createElement('iframe');
//    io.id = frameId;
//    io.name = frameId;
//   }else if(jQuery.browser.version=="6.0" || jQuery.browser.version=="7.0" || jQuery.browser.version=="8.0"){
//     var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
//     if(typeof uri== 'boolean'){
//      io.src = 'javascript:false';
//     }
//     else if(typeof uri== 'string'){
//      io.src = uri;
//     }
//   }
//  }
      if(window.ActiveXObject) { 
       if(jQuery.browser.version=="9.0" || jQuery.browser.version=="10.0"){ 
            var io = document.createElement('iframe'); 
            io.id = frameId; 
            io.name = frameId; 
        }else if(jQuery.browser.version=="6.0" || jQuery.browser.version=="7.0" || jQuery.browser.version=="8.0"){ 
            var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />'); 
            if(typeof uri== 'boolean'){ 
                io.src = 'javascript:false'; 
            } 
            else if(typeof uri== 'string'){ 
                io.src = uri; 
            } 
        } 
    } 
  else {
   var io = document.createElement('iframe');
   io.id = frameId;
   io.name = frameId;
  }
  io.style.position = 'absolute';
  io.style.top = '-1000px';
  io.style.left = '-1000px';

  document.body.appendChild(io);

  return io; 
    },
    createUploadForm: function(id, fileElementId)
 {
  //create form 
  var formId = 'jUploadForm' + id;
  var fileId = 'jUploadFile' + id;
  var form = $('<form  action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>'); 
  var oldElement = $('#' + fileElementId);
  var newElement = $(oldElement).clone();
  $(oldElement).attr('id', fileId);
  $(oldElement).before(newElement);
  $(oldElement).appendTo(form);
  //set attributes
  $(form).css('position', 'absolute');
  $(form).css('top', '-1200px');
  $(form).css('left', '-1200px');
  $(form).appendTo('body');  
  return form;
    },
 addOtherRequestsToForm: function(form,data)
 {
  // add extra parameter
  var originalElement = $('<input type="hidden" name="" value="">');
  for (var key in data) {
   name = key;
   value = data[key];
   var cloneElement = originalElement.clone();
   cloneElement.attr({'name':name,'value':value});
   $(cloneElement).appendTo(form);
  }
  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);
  if ( s.data ) form = jQuery.addOtherRequestsToForm(form,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
          {
           $(io).remove();
           $(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 io = $('#' + frameId);
   var form = $('#' + formId);
   $(form).attr('action', s.url);
   $(form).attr('method', 'POST');
   $(form).attr('target', frameId);
            if(form.encoding)
   {
                form.encoding = 'multipart/form-data';    
            }
            else
   {    
                form.enctype = 'multipart/form-data';
            }   
            $(form).submit();

        } catch(e)
  {   
            jQuery.handleError(s, xml, null, e);
        }
        if(window.attachEvent){
            document.getElementById(frameId).attachEvent('onload', uploadCallback);
        }
        else{
            document.getElementById(frameId).addEventListener('load', uploadCallback, false);
        }   
        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" )
         var str = r.responseText.toString();
         return str.substring(str.indexOf("{"),str.indexOf("}")+1)
   //alert()
           // eval( "data = (" + response +")");
        // evaluate scripts within html
        if ( type == "html" )
            jQuery("<div>").html(data).evalScripts();
   //alert($('param', data).each(function(){alert($(this).attr('value'));}));
        return data;
    }
})

 

分享到:
评论

相关推荐

    方便自己下载的ajaxfileupload.js备份

    该资源提供了两个核心文件,即jquery.min.js和ajaxfileupload.js,它们共同构成了一个简单的图片上传解决方案。 首先,`jquery.min.js`是jQuery库的压缩版,这是一个广泛使用的JavaScript库,简化了DOM操作、事件...

    ajaxfileupload.js完整使用全套(带对应jquery+demo)

    `ajaxfileupload.js`是一个JavaScript库,用于实现异步文件上传功能,避免了传统文件上传时页面刷新的问题。它与jQuery的结合使得文件上传更加简便、高效。 首先,我们来理解`ajaxfileupload.js`的核心概念。这个...

    ajaxfileupload.js用于文件上传

    ajaxfileupload.js用于文件上传

    Jquery上传插件文件ajaxfileupload.js 修复版下载

    使用Jquery做上传文件处理时,用到了ajaxfileupload.js 这个第三方代码,但是这个js几乎就是半成品,问题很多。现在整理如下并附修复版的ajaxfileupload.js下载。 问题: 1:无法带参数提交,只能上传文件; 2:...

    解决ajaxfileupload.js在IE9,firefox下文件超大无效

    ### 解决AjaxFileUpload.js在IE9与Firefox下文件过大导致无效问题 #### 背景介绍 在处理Web前端的文件上传功能时,我们经常会遇到浏览器兼容性问题,尤其是在处理较旧版本浏览器如IE9及Firefox时。本文将详细介绍...

    ajaxfileupload.js

    AjaxFileUpload.js,正如其名,是一款专为前端文件上传设计的JavaScript库,它巧妙地利用了Ajax技术,实现了无刷新的文件上传,极大地提升了用户体验。 AjaxFileUpload.js的核心理念在于异步通信。传统的文件上传...

    ajaxfileupload.js 异步上传jquery插件

    **AjaxFileUpload.js** 是一个基于 jQuery 的异步文件上传插件,它允许用户在不刷新页面的情况下上传文件,提供了一种便捷、高效的文件交互体验。这个插件广泛应用于网页应用,尤其是那些需要用户交互频繁上传文件的...

    ajaxFileUpload.js

    **AjaxFileUpload.js** 是一个JavaScript库,专为在网页上实现异步文件上传而设计。这个库利用了Ajax技术,使得用户可以在不刷新整个页面的情况下上传文件,提供了更好的用户体验。AjaxFileUpload.js通常与jQuery库...

    ajaxfileupload.js 兼容ie

    ajaxfileupload.js 兼容IE

    JSP使用ajaxFileUpload.js实现跨域问题.docx

    ### JSP 使用 ajaxFileUpload.js 实现跨域问题解析 #### 一、背景与概述 在Web开发中,跨域问题一直是开发者面临的一个常见难题。跨域问题是由于浏览器的同源策略导致的,该策略限制了一个域下的JavaScript脚本...

    使用ajaxFileUpload.js上传文件

    `AjaxFileUpload.js`是一个JavaScript库,它允许我们实现异步(Ajax)文件上传,无需刷新页面,提供了更为友好的用户体验。本篇文章将深入探讨`AjaxFileUpload.js`的工作原理、使用方法及其相关知识点。 `...

    ajaxfileupload.js(兼容IE浏览器)

    优化过的ajaxfileupload.js文件 兼容IE,但是用的时候需要引用jquery1.8左右 不能用1.9以上版本

    ajaxfileupload.js文件下载

    此为前端进行文件上传,使用Ajax方式提交的js插件,使用方便简洁,开发很高效。

    ajaxfileupload.js 上传文件不报错版本! 放心使用

    ajaxfileupload.js 上传文件不报错版本! 放心使用!! 平时自己用的

    一个支持Jquery3 的ajaxfileupload.js

    通过修改终于使得ajaxfileupload兼容jquery3,代码已上线运行。

    jquery-1.2.1.js和ajaxfileupload.js

    今天我们要讨论的是两个非常著名的JavaScript文件:“jquery-1.2.1.js”和“ajaxfileupload.js”。这两个文件在Web开发中有着重要的作用,特别是在处理异步文件上传和页面交互方面。 首先,我们来了解“jquery-...

Global site tag (gtag.js) - Google Analytics