`
jxh118
  • 浏览: 124135 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

ExtJS+Jquery实现文件上传

    博客分类:
  • Ajax
阅读更多
jQuery.extend({

    createUploadIframe: function(id, uri){

            var frameId = 'jQuery' + id;
            
            if(window.ActiveXObject) {
                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

    },

    ajaxUpload: 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()
        io = jQuery.createUploadIframe(id, s.secureuri)

        // 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){
            try {
                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;
            }
            catch(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(){ document.body.removeChild(io); }, 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 frameId = 'jQuery' + id;
            var io = document.getElementById(frameId);
            // Initialize the HTML form properties in case they are
            // not defined in the HTML form.
            s.uploadform.action = s.url;
            s.uploadform.method = 'POST';
            s.uploadform.target = frameId;

			// Add extra data that may have been already passed.
			if (s.data) {
				var oEls = s.data.split('&');
				for (var i=0; i < oEls.length; i++ ){
					var thisEl = oEls[i].split('=');
					var thisFormEl = document.createElement('input');
					thisFormEl.type = 'hidden';
					thisFormEl.name = thisEl[0];
					thisFormEl.value = thisEl[1];
					s.uploadform.appendChild(thisFormEl);
				}
				
			}


            if(s.uploadform.encoding){
                // IE does not respect property enctype for HTML forms.
                // Instead use property encoding.
                s.uploadform.encoding = 'multipart/form-data';
            }
            else{
                s.uploadform.enctype = 'multipart/form-data';
            }

            s.uploadform.submit();

        } catch(e) {
            jQuery.handleError(s, xml, null, e);
        }

        if(window.attachEvent){
            io.attachEvent('onload', uploadCallback);
        }
        else{
            io.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" )
            eval( "data = " + data );

        // evaluate scripts within html
        if ( type == "html" )
            jQuery("<div>").html(data).evalScripts();

        return data;
    }
})

Ext.lib.Ajax.formRequest = function(form, uri, cb, data, isUpload, sslUri){
    var createComplete = function(cb){
         return function(xhr, status){
            if((status == 'error' || status == 'timeout') && cb.failure){
                cb.failure.call(cb.scope||window, {
                    responseText: xhr.responseText,
                    responseXML : xhr.responseXML,
                    argument: cb.argument
                });
            }else if(cb.success){
                cb.success.call(cb.scope||window, {
                    responseText: xhr.responseText,
                    responseXML : xhr.responseXML,
                    argument: cb.argument
                });
            }
         };
    };

			if (isUpload) {
				jQuery.ajaxUpload({
					uploadform: form,
					data: data,
					url: uri,
					secureuri: sslUri,
					complete: createComplete(cb)
				});
			} else {
	            jQuery.ajax({
	                type: Ext.getDom(form).method ||'POST',
	                url: uri,
	                data: jQuery(form).formSerialize()+(data?'&'+data:''),
	                timeout: cb.timeout,
	                complete: createComplete(cb)
	            });
			}
        };

 

分享到:
评论
1 楼 xiejin2008 2008-09-05  
请问iframe好用吗?

相关推荐

    ajax + jquery + uploadbean

    总结来说,Ajax负责异步数据交换,jQuery简化了Ajax的使用,UploadBean处理服务器端的文件上传逻辑,而ExtJS则提供了一整套完善的前端开发框架,包括对文件上传的支持。这三者结合,可以构建出高效、用户友好的文件...

    ajax相关资源,js+ajax+Jquery

    **AJAX高级程序设计.exe**可能是一本关于Ajax高级编程的电子书,深入讲解了如何利用Ajax实现更复杂的功能,如文件上传、长轮询、WebSockets等。 **javascript高级教程.pdf**则可能是一份详尽的JavaScript教程,覆盖...

    extjs简单示例。带程序。

    在EXTJS与asp.net的整合中,通常会使用asp.net的Ajax控件工具包或者jQuery的Ajax方法来实现前后端的数据交换。EXTJS的Store组件可以连接到asp.net的Web服务或WCF服务,通过JSON或XML格式交换数据。此外,EXTJS的...

    ExtJS4中文教程2 开发笔记 chm

    ExtJS4学习笔记(十三)---上传文件(upload) ExtJS4学习笔记(十二)---选项卡(tabs) ExtJS4学习笔记(十五)---Ext.data.Model ExtJS4学习笔记(十六)---Combobox三级联动 ExtJS4学习笔记(十四)--- ComponentQuery ExtJS4...

    Extjs部署,包括Extjs部署所需的库和文件

    在深入探讨Extjs部署及其所需库与文件的过程中,我们首先需理解...总之,Extjs的部署不仅仅是将文件上传到服务器那么简单,它需要仔细规划和细致操作,确保每个环节都符合最佳实践,从而实现高效、稳定的前端应用。

    C#+读取文本文件+字符编码

    总的来说,无论是在C#、ASP.NET、ExtJS、jQuery还是Android中,理解和处理字符编码都是至关重要的,它关系到文本数据的正确性和一致性。在实际开发中,开发者需要根据项目需求选择合适的编码方式,并确保在整个数据...

    spket插件extjs3包

    3. **安装插件**:进入Eclipse的“帮助”(Help)菜单,选择“安装新软件”(Install New Software)选项,然后点击“添加”(Add)按钮,输入插件的更新地址或者上传本地的SPket安装文件。 4. **选择插件组件**:在...

    ext_image.rar_ext_image_extjs_extjs 4.2 img_extjs image_extjs图片上

    在ExtJS 4.2中,上传组件可能需要配合第三方插件,如SWFUpload或jQuery File Upload,以实现浏览器兼容性和异步上传。这些插件会处理文件选择、进度显示、错误处理等复杂逻辑。 删除图片的功能可能需要结合服务器端...

    JQERY 批量上传

    批量上传时,要考虑文件大小限制、类型检查,防止恶意文件上传。同时,优化上传性能,如使用断点续传、文件切片等技术,确保用户体验。 综上所述,利用jQuery实现批量上传图片涉及到HTML5的新特性、File API、Ajax...

    ext 文件输入域控件

    虽然链接无法直接访问,但我们可以根据EXTJS的一般用法推测文章可能讲解了如何配置和使用EXTJS的文件输入域控件,包括设置控件的样式、监听事件(如文件选择后触发的事件)、处理文件上传逻辑以及与服务器端的交互等...

    11款基于Javascript的文件管理器

    - **上传能力**: 支持文件上传,便于快速添加新文件至服务器。 - **创建新内容**: 可以直接通过界面上创建新文件或目录。 #### 2. Control.Filebrowser **简介**: 采用Mootools框架开发的一款文件浏览器控件,提供...

    项目涉及到的文件收集_未整理

    8. **UploadDialog**:这可能是一个文件上传对话框的实现,常见于GUI应用中,用户通过对话框选择文件进行上传。可能涉及到的技术有Qt、wxWidgets或Java Swing等桌面开发框架。 9. **Swfupload**:Swfupload是一个旧...

    四个不错的ajax文件管理器

    - **Ajax库**:通常使用jQuery、 Prototype或ExtJS等库简化Ajax调用,实现异步通信。 - **数据库**:有时用于存储用户信息、权限设置等,与文件系统结合提供更高级的服务。 3. **ft2-2.5.7.php 和 ft2.php**: ...

    5款Ajax 文件上传控件

    5. **jQuery: Ajax file upload**:这个jQuery插件简化了多文件上传操作,用户可以在不刷新页面的情况下轻松上传文件。更有趣的是,它可以将任何HTML元素作为触发文件选择的按钮,这样设计不仅灵活,也更加符合现代...

    C#+存取SqlServer中的Image类型

    在前端,jQuery或ExtJS可以帮助你实现动态加载和显示数据库中的图片。 综上所述,C#通过ADO.NET库可以方便地存取SQL Server中的`Image`类型数据。在实际开发中,结合其他技术如ASP.NET、jQuery和ExtJS,可以构建...

    input file的默认value清空与赋值方法

    文章中还提到了通过触发`onchange`事件的方式,即在文件选择器选择新文件后自动清除之前的默认值,这要求对用户的行为有一定的预判,确保在文件选择后能够触发相应的JavaScript函数。 整体来看,这些方法并非标准...

    用JS-EasyUi做的web前端

    它借鉴了EXTJS的许多优秀特性,但在开发效率和灵活性上更胜一筹,特别适合对JavaScript有一定基础的开发者使用。在使用EasyUI进行web前端开发时,主要涉及以下几个关键知识点: 1. **jQuery基础知识**:EasyUI 基于...

    应届程序员简历.docx

    6. **责任描述**:在项目中,负责文档管理模块,使用uploadly实现文件上传和续传,MD5加密下载,索引支持高级搜索,以及新建文件夹等功能。权限管理采用RBAC模型,通过过滤器将权限赋予角色,用户与角色关联,实现...

    JAVA-ziyuan

    - 使用MVC框架进行开发,其中M层可以使用Hibernate或MyBatis等ORM技术,V视图层使用FreeMarker模板、JQuery或ExtJS等,C控制层必须使用Spring框架; - 选择题目范围,进行WEB应用程序设计和开发。 ### 二、技术栈...

Global site tag (gtag.js) - Google Analytics