文件上传字段Ext.form.field.File(扩展Text)

本文介绍如何使用ExtJS框架中的Ext.form.field.File组件实现文件上传功能。通过具体实例展示了文件上传面板的创建过程,包括配置文件上传按钮、定义上传处理函数及与服务器交互等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

133958_ccLU_1773772.png

<div id="uploadForm" class="w_320">
    <h2>文件上传字段</h2>
</div>
Ext.onReady(function(){
    //Ext表单
    
    //Ext.form.Basic 基本表单组件二(拾取器字段Picker)
    //开发中使用表单面板组件Ext.form.Panel,它本质是一个标准的(面板)Ext.panel.Panel,它内置并自动创建了Ext.form.Basic基本表单组件
    //与原始表单主要3点不同(1.提交方式 2.表单验证 3.表单组件)
    //1.提交方式(同步---异步)
    //2.表单验证(需要手动验证----配置即可使用)
    //3.表单组件(基本的组件------扩展的功能强大的组件)
    
    //Picker抽象类,具有唯一的触发按钮用于在字段下方动态弹出面板
    //拾取器组件如:(1.ComboBox,2.Date,3.Time)
    
    //初始化信息提示功能
    Ext.QuickTips.init();
    
    //文件上传字段Ext.form.field.File(扩展Text)
    var uploadForm = Ext.create("Ext.form.Panel",{
        title : 'Ext.form.field.File示例',
        width : 300,
        height : 100,
        renderTo : 'uploadForm',
        farme : true,
        bodyStyle : 'padding:5px',
        defaults : {//统一设置字段属性
            width : 150,
            labelWidth : 50,
            labelSeparator : ': ',
            labelAlign : 'left',
            allowBlank : false,//不允许为空
            msgTarget : 'side'//在字段的右边显示提示信息
        },
        items : [{
            name : 'myPhoto',
            fieldLabel : '照片',
            xtype : 'filefield',
            anchor : '100%',
            buttonText : '选择照片...'
        }],
        buttons : [{
            text : '上传文件',
            handler : uploadMyFile
        }]
        
    });
    
    //上传文件回调函数
    function uploadMyFile(){
        var form = uploadForm.getForm();
        if(form.isValid()){
            form.submit({
                url : '../upload.jsp',
                waitMsg : '正在上传照片文件请稍后...',
                success : function(fp,o){
                    console.info(o);
                    if(o.result.success){
                        Ext.Msg.alert('提示信息','你的照片文件"'+o.result.file+'"已经上传成功');
                    }else {
                        Ext.Msg.alert('上传失败!');
                    }
                }
            });
        }
    }
    
});
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
    DiskFileUpload upload = new DiskFileUpload();
    upload.setHeaderEncoding("utf8");
    List items = upload.parseRequest(request);
    ListIterator listIterator = items.listIterator();
    String fileName = "";
    while(listIterator.hasNext()){
        FileItem item = (FileItem)listIterator.next();
        if(!item.isFormField()){
            fileName = item.getName();
            fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
        }
    }
    String msg = "{success:true,file:'"+fileName+"'}";
    response.getWriter().write(msg);
%>

    1.HTML文件   2.JS文件  3. JSP文件

转载于:https://my.oschina.net/u/1773772/blog/612949

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值