Extjs文件上传

1.前台js代码

var formPanel = new Ext.form.FormPanel({
        title: '标识',
        labelAlign: 'right',
        buttonAlign: 'center',
        labelWidth: 80,
        defaults: { autoWidth: true },
        padding: 10,
        frame: false,
        border: false,
        autoScroll: true,
        fileUpload: true,
        items: [       
                {
                    layout: 'column',
                    border: false,
                    columnWidth: 1,
                    items:
                     [
                      {
                          columnWidth: 1,
                          layout: 'form',
                          border: false,
                          items: [MLCBMXX,MLCGYS,
                                   [
                                     {
                                         xtype: 'textfield',
                                         id: 'txtFile',
                                         fieldLabel: '仓储数据文件',
                                         inputType: 'file',
                                         buttonText: '浏览',
                                         width: 258
                                     }
                                    ]
                                 ]
                      }
                     ]
                }        
               ],
          bbar: [
                 {
                    text:'上传',
                    iconCls: 'icon_uploading',
                    handler: function() {
                    
                    if (!formPanel.getForm().isValid()) {
                        Ext.MessageBox.alert("信息", "表单输入验证失败,请正确填写完整!");
                        return;
                    }
 
                        formPanel.form.submit({
                            waitMsg: 'Uploading ....',
                            url: 'Upload.ashx',
                            method: "POST",
                            success: function(form, action) {
                             
                            },
                            failure:function(form,action){

                            }

                            }
                            
                        });
                        
                    }                 
                 }
                ]               
     });

2.后台"一般处理程序"Upload.ashx

<%@ WebHandler Language="C#" Class="Upload" %>
using System;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Data;
using System.Text;
using Inch.Base.Web;
using Inch.Base.Database;

public class Upload : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        FileInfo info = new FileInfo();
        try
        {
            info.success = true;   
            int iTotal = context.Request.Files.Count;
            if (iTotal == 0)
                return;
            HttpPostedFile file = context.Request.Files[0];
            int len = file.ContentLength;
            if (len > 0 && !string.IsNullOrEmpty(file.FileName))
            {
                string parentPath = HttpContext.Current.Server.MapPath("~/upload/");
                if (!Directory.Exists(parentPath))
                {
                    Directory.CreateDirectory(parentPath);
                }
                file.SaveAs(Path.Combine(parentPath, Path.GetFileName(file.FileName)));

                info.path = Path.Combine(parentPath, Path.GetFileName(file.FileName));
                info.name = Path.GetFileName(file.FileName);
                info.size = len.ToString();
                info.isError = false;
            }
        }
        catch (Exception e)
        {
            info.errorMessage = e.Message;
            info.isError = true;
        }
        finally
        {
            JavaScriptSerializer j = new JavaScriptSerializer();
            context.Response.Write(j.Serialize(info));
            context.Response.End();
        }
    }


    public string json(System.Data.DataTable data)
    {
        int colums = data.Columns.Count;
        StringBuilder str = new StringBuilder();
        foreach (DataRow row in data.Rows)
        {
            string rowStr = "";
            foreach (DataColumn Column in data.Columns)
            {
                rowStr = rowStr + ",'" + row[Column.ColumnName] + "'";
            }
            if (rowStr != "")
                rowStr = rowStr.Substring(1);
            str.Append(",").Append("[").Append(rowStr).Append("]");
        }
        if (str.ToString() != null && str.ToString() != "")
            return "[" + str.ToString().Substring(1) + "]";
        else
            return "[]";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public class FileInfo
    {
        public string name;
        public string path;
        public string size;
        public string tp;
        public bool success;
        public string errorMessage;
        public bool isError;
    }
}
posted @ 2012-06-05 15:31  angus_csh  阅读(4484)  评论(0编辑  收藏  举报