Asp.net ExtJS 实现文件上传

	function  upfile3()
	{
	   		var winUpload3	;
			if(!winUpload3){
			
			
			
	     winUpload3 = new Ext.Window({
                    title: '资源上传',
                    width: 400,
                    height: 200,
                    minWidth: 300,
                    minHeight: 100,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                 //   items: formUpload,
				   items:[
				     new Ext.form.FormPanel({
                baseCls: 'x-plain',
                labelWidth: 80,
                fileUpload: true,
                defaultType: 'textfield',
                items: [{
                    id: "upExcel",
                    xtype: 'textfield',
                    emptyText: '选择日报',
                    fieldLabel: '文 件',
                    name: 'upExcel',
                    inputType: 'file',
                    allowBlank: false,
                    blankText: '请上传文件',
                    anchor: '90%'
                }]
                })
				   
				   
				   
				   ],
                    buttons: [{
                        text: '上 传',
                        handler: function() {
                            if (formUpload.form.isValid()) {
                                formUpload.getForm().submit({
                                    waitTitle: "请销等……",
                                    url: 'http://localhost/Kzdhandler/extjsuploadfile.aspx',
                                    waitMsg: '上传中……',
                                    params: { filetype: '.xls' },

                                    success: function(form, action) {
                                        form.reset();
                                        Ext.Msg.alert('成功', action.result.msg);
                                        winUpload3.hide();
                                    },
                                    failure: function(form, action) {
                                    Ext.Msg.alert('提示', action.result.msg);
                                    }
                                })
                            }
                        }
                    }, {
                        text: '取 消',
                        handler: function() { winUpload3.hide(); }
     }]
                    });
					

	}
	winUpload3.show();
	
	
	}

 

ext.js 端代码

 

服务器端代码 ,hander中添加一个aspx 页面

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class extjsuploadfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["filetype"].ToString() == ".xls")
        {
            HttpPostedFile uploadFile = Request.Files["upExcel"];
            if (uploadFile == null)
            {
                Response.Write("{success:false,msg:'未找到上传文件'}");
                Response.End();

            }
            int lastIndex = uploadFile.FileName.LastIndexOf(".");
            string filename = uploadFile.FileName.ToString();

            string filenametype = uploadFile.FileName.Substring(lastIndex);
            if (filenametype != ".doc")
            {
                Response.Write("{success:false,msg:'文件格式不对"+filename+"'}");
                Response.End();
            }
           // filename = filename.Substring(0, 2);
            int fileindex = filename.LastIndexOf("\\");
            filename = filename.Substring(fileindex + 1, filename.Length-fileindex-1);
            string path = "File/" + filename;
            uploadFile.SaveAs(Server.MapPath(path));
            Response.Write("{success:true,msg:'上传成功'}");
            Response.End();


        }
    }
}

注意问题  中文名称乱码

解决方法

在webconfig 中添加

<configuration>

<location path="extjsuploadfile.aspx">
    <system.web>
      <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
    </system.web>
  </location>

 <system.web>

。。。。。。

 

posted on 2012-10-20 11:16  markygis  阅读(238)  评论(0)    收藏  举报