邮件系统使用的上传附件方法

时间:2022-06-02 17:36:56
 

前台HTML代码

<form id="mail" method="post" runat="server">
<table border="0" cellpadding="0" cellspacing="0" style="BORDER-COLLAPSE: collapse" bordercolor="#111111"
width="39%" id="AutoNumber1" height="75">
<tr>
<td width="100%" height="37"><INPUT id="myFile" style="WIDTH: 297px; HEIGHT: 22px" type="file" size="30" name="myFile"
runat="server">&nbsp;
<asp:button id="Upload" runat="server" Text="上传附件"></asp:button></td>
</tr>
<tr>
<td width="100%" height="38">
共计
<asp:textbox id="P_size" runat="server" Width="65px"></asp:textbox>KB&nbsp;&nbsp;&nbsp;
&nbsp;
<asp:dropdownlist id="dlistBound" runat="server"></asp:dropdownlist>&nbsp;&nbsp;
<asp:button id="btnDel" runat="server" Text="删除附件"></asp:button></td>
</tr>
</table>
</form>

 

后台CS代码

public class Upload_Mail : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Upload;
        protected System.Web.UI.WebControls.DropDownList dlistBound;
        protected System.Web.UI.WebControls.TextBox P_size;
        protected System.Web.UI.WebControls.Button btnDel;
        protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
   
        private void Page_Load(object sender, System.EventArgs e)
        {

            if (!IsPostBack)
            {
                //没有附件的状态
                dlistBound.Items.Clear();
                ArrayList arr = new ArrayList();
                arr.Add("--没有附件--");
                dlistBound.DataSource = arr ;
                dlistBound.DataBind();
                P_size.Text = "0";
            }
        }

        private void Upload_Click(object sender, System.EventArgs e)
        {
            if(myFile.PostedFile !=null)
            {
                HttpFileCollection files  = HttpContext.Current.Request.Files;
                HttpPostedFile postedFile = files[0];
                string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                string path = Request.PhysicalApplicationPath+@"UploadMail/"+ fileName;
                postedFile.SaveAs(path);

                //数组对上存附件进行实时绑定
                if((string)Session["udMail"]==null)
                {
                    Session["udMail"] = fileName;
                }
                else
                {
                    Session["udMail"] = (string)Session["udMail"]+"|"+fileName;
                }

                string[] udMail = Session["udMail"].ToString().Split('|');
                ArrayList list = new ArrayList(udMail);
                list.Reverse();
                udMail=(string[])list.ToArray(typeof(string));
                dlistBound.Items.Clear();
                long dirsize=0;
                for(int i = 0;i<udMail.Length;i++)
                {
                    string  IndexItem =  udMail[i];
                    string  VauleItem = Request.PhysicalApplicationPath+@"UploadMail/"+udMail[i];
                    dlistBound.Items.Add(new ListItem(IndexItem,VauleItem));
                    System.IO.FileInfo mysize = new System.IO.FileInfo(@VauleItem);
                    dirsize += System.Convert.ToInt32(mysize.Length/1024)+1;
                }
                P_size.Text = dirsize.ToString();

            }
        }

        private void btnDel_Click(object sender, System.EventArgs e)
        {
            string trueDelfile = dlistBound.SelectedValue.ToString();
            string Delfile = dlistBound.SelectedItem.ToString();
            usageIO.DeletePath(trueDelfile);

            if(Session["udMail"] != null)
            {
                int index = Session["udMail"].ToString().IndexOf("|");
                if(index == -1)
                {
                    Session["udMail"] = null;
                    dlistBound.Items.Clear();
                    dlistBound.Items.Add("--没有附件--");
                    P_size.Text = "0";
                }
                else
                {

                    string[] udMail = Session["udMail"].ToString().Split('|');
                    ArrayList values = new ArrayList(udMail);
                    values.Remove(Delfile);
                    string s = null;
                    for(int i=0;i<values.Count;i++)
                    {
                        if(values.Count!=0)
                        {
                            s += values[i].ToString()+"|";
                        }
                    }
                    if(s!=""||s!=null)
                    {
                        s =  s.TrimEnd('|');
                    }
                    Session["udMail"] = s;

                    string[] uMail = Session["udMail"].ToString().Split('|');
                    ArrayList list = new ArrayList(uMail);
                    list.Reverse();
                    uMail=(string[])list.ToArray(typeof(string));
                    dlistBound.Items.Clear();
                    long dirsize=0;
                    for(int i = 0;i<uMail.Length;i++)
                    {
                        string  IndexItem =  uMail[i];
                        string  VauleItem = Request.PhysicalApplicationPath+@"UploadMail/"+uMail[i];
                        dlistBound.Items.Add(new ListItem(IndexItem,VauleItem));

                        System.IO.FileInfo mysize = new System.IO.FileInfo(@VauleItem);
                        dirsize += System.Convert.ToInt32(mysize.Length/1024)+1;
                    }
                    P_size.Text = dirsize.ToString();
                }
            }

        }

        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.Upload.Click += new System.EventHandler(this.Upload_Click);
            this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }