zoukankan      html  css  js  c++  java
  • 上传代码

     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>
    
    
    
     


  • 相关阅读:
    如何编译树莓派内核
    代码导出Reporting Services报表文件
    Bit-Coin收入的一分钱
    如何在树莓派上运行雷神之锤III
    新树莓派入手
    如何通过PowerShell在Visual Studio的Post-build中预热SharePoint站点
    每日一题20201218(389. 找不同)
    每日一题20201217(714. 买卖股票的最佳时机含手续费)
    每日一题20201216(290. 单词规律)
    每日一题20201215(738. 单调递增的数字)
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/4482031.html
Copyright © 2011-2022 走看看