zoukankan      html  css  js  c++  java
  • asp .net 多文档上传控件

         在moss要做多文档上传,moss使用的多文档上传控件,必须要客户端安装office组件,这就限制了这个组件的使用,只有自己写一个上传控件了,在google一下,发现codeproject上有一篇文章正好符合我的要求,参考这个文章封装成了一个控件。

    界面如下:

    image image image

    控件的公共属性:

    image

    其中ButtonCSS是设置在按钮上的样式,此样式设置在TD上

    InputFileCSS是设置选择文件控件的样式

    ListBoxCSS是设置要上载的文件框的样式,此样式设置在TD上

    FileList是获取要上载的文件列表,类型为:System.Web.UI.HtmlControls.HtmlInputFile[]

    FilesCount是上载文件的数目

    UpLoadButtonEnable是启用控件中的上载按钮设置,当设置为true时,就必须设置属性“UpLoadPath“

    UpLoadPath是上载文件的路径

    源码如下:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    
    namespace CDMTC.SCTelKM.Repository.code
    {
        public class MultipleFileUpload : WebControl
        {
            #region 公共属性
            protected System.Web.UI.WebControls.ListBox ListBox1;
            protected System.Web.UI.WebControls.Button AddFile;
            protected System.Web.UI.WebControls.Button RemvFile;
            protected System.Web.UI.HtmlControls.HtmlInputFile FindFile;
            protected System.Web.UI.HtmlControls.HtmlInputButton Upload;
            protected System.Web.UI.WebControls.Label lblError;
            // public ArrayList files = new ArrayList();
            static private List<HtmlInputFile> hif = new List<HtmlInputFile>();
            private int filesUploaded = 0;
            public int FilesCount
            {
                get
                {
                    return hif.Count;
                }
            }
            public MultipleFileUpload()
            {
                lblError = new Label();
            }
            private bool _UpLoadButttonEnable;
            /// <summary>
            /// 是否启用默认的上载文件操作
            /// </summary>
            public bool UpLoadButttonEnable
            {
                set
                {
                    _UpLoadButttonEnable = value;
                }
                get
                {
                    return _UpLoadButttonEnable;
                }
            }
    
            private string _UpLoadPath;
            /// <summary>
            /// 上载文件的路径,物理路径,如:c:\temp
            /// </summary>
            public string UpLoadPath
            {
                set
                {
                    _UpLoadPath = value;
                }
                get
                {
                    return _UpLoadPath;
                }
            }
    
            public HtmlInputFile[] FileList
            {
                get
                {
                    return hif.ToArray();
                }
            }
            #endregion
    
            #region Style
            private string _ListBoxCSS = "";
            [Category("样式定义")]
            [Browsable(true)]
            [DisplayName("文件列表框样式")]
            [Description("此样式应用于TD上")]
            public string ListBoxCSS
            {
                set
                {
                    _ListBoxCSS = value;
                }
                get
                {
                    return _ListBoxCSS;
                }
            }
            private string _ButtonCSS = "";
            [Category("样式定义")]
            [Browsable(true)]
            [DisplayName("按钮样式")]
            [Description("此样式应用于TD上")]
            public string ButtonCSS
            {
                set
                {
                    _ButtonCSS = value;
                }
                get
                {
                    return _ButtonCSS;
                }
            }
            private string _InputFileCSS = "";
            [Category("样式定义")]
            [Browsable(true)]
            [DisplayName("文件选择控件样式")]
            [Description("此样式应用于TD上")]
            public string InputFileCSS
            {
                set
                {
                    _InputFileCSS = value;
                }
                get
                {
                    return _InputFileCSS;
                }
            }
            #endregion
    
    
            protected override void OnLoad(EventArgs e)
            {
                if (_UpLoadButttonEnable && string.IsNullOrEmpty(_UpLoadPath))
                {
                    throw new Exception("设置了启用内部上传操作时,必须设置上传路径(UpLoadPath)");
                }
                CreateControls();
                if (!Page.IsPostBack)
                {
                    hif.Clear();
                    ListBox1.Items.Clear();
                }
                if (hif.Count == 0)
                    ListBox1.Items.Clear();
                lblError.Text = "";
                base.OnLoad(e);
            }
            private void CreateControls()
            {
                Table tb = new Table();
                tb.Attributes.Add("width", "100%");
                tb.Attributes.Add("border", "0");
    
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                FindFile = new HtmlInputFile();
                if (!string.IsNullOrEmpty(_InputFileCSS))
                {
                    tc.Attributes.Add("class", _InputFileCSS);
                }
                tc.Controls.Add(FindFile);
    
                AddFile = new Button();
                AddFile.Text = "添加";
                AddFile.Click += new EventHandler(AddFile_Click);
                tc.Controls.Add(AddFile);
                RemvFile = new Button();
                RemvFile.Text = "删除";
                RemvFile.Click += new EventHandler(RemvFile_Click);
                tc.Controls.Add(RemvFile);
    
                if (_UpLoadButttonEnable)
                {
                    Upload = new HtmlInputButton();
                    Upload.Value = "上载";
                    Upload.ServerClick += new EventHandler(Upload_ServerClick);
                    tc.Controls.Add(Upload);
                }
                if (!string.IsNullOrEmpty(_ButtonCSS))
                {
                    tc.Attributes.Add("class", _ButtonCSS);
                }
                tr.Cells.Add(tc);
    
                tb.Rows.Add(tr);
    
                tr = new TableRow();
                tc = new TableCell();
                ListBox1 = new ListBox();
                tc.Controls.Add(ListBox1);
                tr.Cells.Add(tc);            
                ListBox1.Height = 100;
               
    
                if (!string.IsNullOrEmpty(_ListBoxCSS))
                {
                    tc.Attributes.Add("class", _ListBoxCSS);
                }
    
                tb.Rows.Add(tr);
                tr = new TableRow();
                tc = new TableCell();
                tc.Controls.Add(lblError);
                tr.Cells.Add(tc);
                tb.Rows.Add(tr);
                Controls.Add(tb);
            }
    
            protected override void OnPreRender(EventArgs e)
            {
                if (hif.Count == 0)
                {
                    ListBox1.Items.Clear();
                    ListBox1.Width = 350;
                    ListBox1.ToolTip = "此框列出要上载的文档列表";
                }
                else
                {
                    StringBuilder str = new StringBuilder();
                    foreach (ListItem li in ListBox1.Items)
                    {
                        str.Append(li.Text);
                        str.Append("\n");
                    }
                    ListBox1.ToolTip = str.ToString().TrimEnd('\n');
                }
                base.OnPreRender(e);
                HtmlForm form = this.Page.Form;
                if ((form != null) && (form.Enctype.Length == 0))
                {
                    form.Enctype = "multipart/form-data";
                }
            }
    
            void Upload_ServerClick(object sender, EventArgs e)
            {
                string baseLocation = _UpLoadPath;
                string status = "";
    
    
                if ((ListBox1.Items.Count == 0) && (filesUploaded == 0))
                {
                    lblError.Text = "错误 - 请先选择一个要上传的文件.";
                    return;
    
                }
                else
                {
                    foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
                    {
                        try
                        {
                            string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                            HIF.PostedFile.SaveAs(baseLocation + fn);
                            filesUploaded++;
                            status += fn + "<br>";
                        }
                        catch (Exception err)
                        {
                            lblError.Text += "保存文件到:“ " + baseLocation + "”出错<br>错误描述:" + err.ToString();
                        }
                    }
    
                    if (filesUploaded == hif.Count)
                    {
                        lblError.Text += string.Format("{0}文件已经上传成功<br>{1}", filesUploaded, status);
                    }
                    hif.Clear();
                    ListBox1.Items.Clear();
                }
            }
    
            void RemvFile_Click(object sender, EventArgs e)
            {
                if (ListBox1.Items.Count != 0)
                {
                    hif.RemoveAt(ListBox1.SelectedIndex);
                    ListBox1.Items.Remove(ListBox1.SelectedItem.Text);
                }
            }
    
            void AddFile_Click(object sender, EventArgs e)
            {
                if (Page.IsPostBack == true)
                {
                    if (string.IsNullOrEmpty(FindFile.PostedFile.FileName))
                    {
                        lblError.Text += "请选择要添加的文件!";
                        return;
                    }
                    foreach (ListItem li in ListBox1.Items)
                        if (li.Text == FindFile.PostedFile.FileName)
                        {
                            lblError.Text += "文件已经加入到上载列表中,请不要重复加载!";
                            return;
                        }
                    hif.Add(FindFile);
                    ListBox1.Items.Add(FindFile.PostedFile.FileName);
                }
                else
                {
    
                }
            }
            public void Clear()
            {
                hif.Clear();
                // ListBox1.Items.Clear();
            }
        }
    }
    
  • 相关阅读:
    转:配置nodemanager启动weblogic服务器
    SUSE Linux下新建Weblogic 10.3非admin服务
    转weblogic 10.3新建域
    mysql 导出慢
    sql 查询效率
    js isnull 赋值方法
    linux 启动weblogic的某服务报错
    linux下oracle 10g的sqlplus无法使用
    union all 取代 select中的case when 提高查询效率
    php版判断是否为ajax请求的小demo
  • 原文地址:https://www.cnblogs.com/LifelongLearning/p/1241761.html
Copyright © 2011-2022 走看看