上传代码

 protected void btnUpload_Click(object sender, EventArgs e)
        {
            Button FU = (Button)sender;
            //文件保存目录路径
            string savePath = "/Upload/Subject/";
            string dirPath = upload + "/Upload/Subject/";
            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");

            //保存路径
            string saveFilePath = "";

            #region 获取当前 FileUpload 的名字
            string getBtnID = FU.ID;
            string GetFuName = "";
            if (getBtnID.IndexOf("Content") > 0)
            {
                GetFuName = "fu_Content_Url";
            }
            else if (getBtnID.IndexOf("_A_") > 0)
            {
                GetFuName = "fu_A_Url";
            }
            else if (getBtnID.IndexOf("_B_") > 0)
            {
                GetFuName = "fu_B_Url";
            }
            else if (getBtnID.IndexOf("_C_") > 0)
            {
                GetFuName = "fu_C_Url";
            }
            else
            {
                GetFuName = "fu_D_Url";
            }

            FileUpload NewFU = new FileUpload();
            foreach (Control control in this.form1.Controls)
            {
                if (control.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")//
                {
                    if (((FileUpload)control).ID == GetFuName)
                    {
                        NewFU = (FileUpload)control;//得到控件
                    }
                }
            }

            #endregion


            //最大文件大小
            int maxSize = 500 * 1024;
            if (!NewFU.HasFile)
            {
                LigerDialog.Warn("请选择要上传的文件", this);
                return;
            }
            string fileName = NewFU.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            if (NewFU.FileBytes.Length > maxSize)
            {
                LigerDialog.Warn("上传的文件大小不能超过500k", this);
                return;
            }

            if (Array.IndexOf(((String)extTable["image"]).Split(,), fileExt.Substring(1).ToLower()) == -1)
            {
                LigerDialog.Warn("上传文件扩展名是不允许的扩展名,只允许" + ((string)extTable["image"]) + "格式", this);
                return;
            }
            try
            {
                //创建文件夹
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }


                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(10, 100) + fileExt;
                saveFilePath = savePath + newFileName;
                string filePath = dirPath + newFileName;
                NewFU.SaveAs(filePath);
                switch (NewFU.ID)
               {
                   case "fu_Content_Url": img_Content.ImageUrl = saveFilePath; break;
                   case "fu_A_Url": img_A.ImageUrl = saveFilePath; break;
                   case "fu_B_Url": img_B.ImageUrl = saveFilePath; break;
                   case "fu_C_Url": img_C.ImageUrl = saveFilePath; break;
                   case "fu_D_Url": img_D.ImageUrl = saveFilePath; break;
               }
            }
            catch
            {
                LigerDialog.Warn("上传失败", this);
            }
        }

       <td width="65%">
                <asp:FileUpload runat="server" ID="fu_Content_Url" />
                &nbsp;<asp:Button runat="server" ID="btn_fu_Content_Url" Text="上传" CssClass="l_button" OnClick="btnUpload_Click" />
                <label class="label_red">
                    不大于500k</label>
                <br />
                <asp:Image runat="server" ID="img_Content" Width="200" Height="150" />
            </td>

 

 

 


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。