图片上传、删除

本文介绍了一个使用ASP.NET实现的简单图片上传和删除功能的具体代码实现过程。该功能包括通过按钮触发文件上传、验证文件类型、保存文件到指定路径以及提供删除已上传图片的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <tr>
                <td align="right" style="width: 101px; height: 31px;">&nbsp;</td>
                <td align="left" style="width: 661px; height: 31px">
                    <asp:TextBox ID="TxtPicPath" runat="server" Enabled="False"></asp:TextBox>
                    <asp:Button ID="BtnDelPic" runat="server" Text="删除首页图片" CausesValidation="False" OnClick="BtnDelPic_Click" Enabled="False" /></td>
            </tr>

 

 

protected void BtnUpload_Click(object sender, EventArgs e)
    {
        //Response.Write("zz");
        //Response.End();

        string filepath = UpLoad(FileUp, "uploads/IndexPic/");

        if (filepath.Length > 4)
        {
            LblUpResult.Text = "上传成功!";
            TxtPicPath.Text = filepath;
            this.BtnDelPic.Enabled = true;
        }
    }

 

 

 if (TxtPicPath.Text != "")
        {
            LblUpResult.Text = "";
            DelFile(TxtPicPath.Text);
            TxtPicPath.Text = "";
        }

using System.IO;

page_load()

{
  if (!IsPostBack) { }

}

#region 删除单个文件
    public static bool DelFile(string Path)
    {
        try
        {

            File.Delete(System.Web.HttpContext.Current.Server.MapPath(Path));
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    #endregion
  

 

 

#region 上传文件
    public string UpLoad(System.Web.UI.WebControls.FileUpload F, string LoadPathe)
    {
        string FilePath = "";
        if (F.HasFile)
        {
            string Filename = F.FileName;//文件名
            string FileExen = Filename.Substring(Filename.LastIndexOf(".") + 1).ToLower();//扩展名

            //string[] AllowExen = new string[] { "gif", "png", "jpg", "bmp", "rar", "zip", "doc" };
            string[] AllowExen = new string[] { "gif", "png", "jpg", "bmp", "swf" };

            int flag = 0;
            for (int i = 0; i < AllowExen.Length; i++)
            {
                if (FileExen.Equals(AllowExen[i]))
                {
                    flag++;
                }
            }
            if (flag > 0)
            {
                string newFileName;
                //上传路径后是否带有"/",没有就加上
                string LoadPath = "./" + LoadPathe;
                /*if (LoadPath == "")
                {
                    LoadPath = "./";
                }
                if (!LoadPath.Substring(LoadPath.Length - 1).Equals("/"))
                {
                    LoadPath += "/";
                }*/
                //生成上传全路径
                newFileName = Server.MapPath(LoadPath) + Filename;


                //Response.Write(newFileName);
                //Response.End();
                if (File.Exists(newFileName))
                {
                    System.Random rd = new Random();
                    string r = rd.Next(10, 99).ToString();
                    newFileName = newFileName.Insert(newFileName.LastIndexOf("."), "_" + r);
                }
               //Response.Write("a");


                F.SaveAs(newFileName);
                FilePath = LoadPathe + newFileName.Replace(System.Web.HttpContext.Current.Server.MapPath(LoadPath), "");

              //Response.Write("b");

            }
        }
        return FilePath;
    }
    #endregion

 

//一下为数据库操作的流程方式(不是针对图片上传)

string sqlStr = "update Product set Product_Hits=Product_Hits+1 where (Product_ID=" + Request.QueryString["ProductID"] + ")";
            DataAccess da = new DataAccess();
            da.Open(DataFactory.DbType.DataBaseType.Sql);
            da.ExcuteNonQuery(sqlStr);
            da.Close();

            SqlConnection sqlcn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            sqlcn.Open();
            SqlDataAdapter sqlda = new SqlDataAdapter("select * from Product,ShopDetail where Product_ID=" + Request.QueryString["ProductID"].ToString(),sqlcn);
            DataSet ds = new DataSet();
            sqlda.Fill(ds,"tb");
            lbtitle.Text = ds.Tables["tb"].Rows[0]["Product_Name"].ToString();
            lbshopname.Text = ds.Tables["tb"].Rows[0]["Shop_ShopName"].ToString();
            lbtime.Text = ds.Tables["tb"].Rows[0]["Product_Time"].ToString();
            lbhits.Text = ds.Tables["tb"].Rows[0]["Product_Hits"].ToString();
            lbcontent.Text = ds.Tables["tb"].Rows[0]["Product_Info"].ToString();
            ImagePhoto.ImageUrl = ds.Tables["tb"].Rows[0]["Product_Photo"].ToString();
            sqlcn.Close();

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值