Extjs文件上传

从今天起我会和大家一块来学习ExtJs的典型操作,前面我写了一些ExtJs的文章,那个系列主要是系统的来介绍Ext如何使用。但是我们知道如果仅仅那样来介绍会遗漏很多实用的内容,因此我今后会继续写一些常用的Ext操作方面的内容,暂且叫做"ExtJs典型用法"系列吧。

今天我们首先看一下Ext中如何上传文件。我们知道在传统的html中有file组件(也就是type为file的input组件),但是我们也可以看到在Ext中是没有相应的组件的。那么我们如何来上传文件呢?在Ext中要完成上传其实很简单,只需要设置FormPanel的fileUpload属性和textfield的inputType属性即可。好,废话不多说,直接看代码吧:

前台代码:Ext.onReady(function(){
                    var fpFileUpload=new Ext.FormPanel({
                        id:'fpFileUpload',
                        frame:true,
                        fileUpload:true,
                        //url:'Default.aspx',
                        items:[
                            {
                                xtype:'textfield',
                                allowBlank:false,
                                fieldLabel:'选择文件',
                                inputType:'file',
                                name:'fileName'
                            }
                        ],
                        buttonAlign:'center',
                        buttons:[
                            {
                                text:'上传',
                                handler:function(){
                                    if(fpFileUpload.form.isValid()){
                                        fpFileUpload.form.submit({
                                            method:'post',
                                            url:'Default.aspx',
                                            waitMsg:'文件上传中...',
                                            success: function() {
                                                Ext.Msg.alert("系统提示", "文件上传成功!");
                                            },
                                            failure: function() {
                                                Ext.Msg.alert("系统提示", "文件上传失败!");
                                            }
                                        });
                                    }else{
                                        Ext.Msg.alert("系统提示","请选择文件后再上传!");
                                    }
                                }
                            },
                            {
                                text:'取消',
                                handler:function(){
                                    winFielUpload.hide();
                                }
                            }
                        ]
                    });
                    var winFielUpload=new Ext.Window({
                        id:'win',
                        title:'文件上传',
                        //****renderTo:'divWindow',//对于window不要使用renderTo属性,只需要调用show方法就可以显示,添加此属性会难以控制其位置
                        width:350,
                        height:100,
                        layout:'fit',
                        autoDestory:true,
                        modal:true,
                        closeAction:'hide',
                        items:[
                            fpFileUpload
                        ]
                    });
                    window.winFielUpload=winFielUpload;
                
            });
            function showWindow(){
                    winFielUpload.show();
            }

后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Serialization;
namespace FileUpload
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Files.Count == 0)
                {
                    return;
                }
                else
                {
                    HttpPostedFile file = Request.Files[0];
                    if (file.ContentLength > 0 && !string.IsNullOrEmpty(file.FileName))
                    {
                        file.SaveAs(Server.MapPath("/UploadFile/" + Path.GetFileName(file.FileName)));
                        //FileInfo fileInfo=new FileInfo(Server.MapPath("/UploadFile/" + Path.GetFileName(file.FileName)));
                        //JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                        Response.Write("{success:true}");//前台就是通过json中success的true活false来判断是否成功的,切记!
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("{success:false}");
            }
            Response.End();
        }
    }
}

这样就可以成功的上传文件了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值