ASP.NET_上传图片,并把图片路径保存到数据库中

//html页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    //asp:Image 控件
        <div>
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/lmage_4.jpeg" Width="150" Height="170"/>  
        </div>


        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
        </div>
    </form>
</body>

</html>

============================================================================

//后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;




public partial class Default2 : System.Web.UI.Page
{
    DBHelper dbhelper = new DBHelper();//我是用Web.config和类写的连接,这处不要复制,建议你们自己写连接
    int id = 0;


    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        bool bok = false;//默认为false
        string path = Server.MapPath("Images");//储存文件夹路径
        if(this.FileUpload1.HasFile)//检测是否有上传文件
        {
            string file = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();//获取文件夹下的文件路径
            string[] allow = new string[] { ".png", ".jpg", ".gif", ".bmp", ".jpeg" };//后缀名数组


            foreach(string s in allow)//读取后缀名数组
            {
                if (s == file) //如果符合数组里的类型
                {
                    bok = true;//bool值为true
                }
            }


            if (bok)//如果为true
            {
                try
                {
                    this.FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);//上传文件
                    this.lbl.Text = "文件" + FileUpload1.FileName + "上传成功!";                   
                }
                catch (Exception ex)
                {
                    this.lbl.Text = "文件上传失败!" + ex.Message;
                }
            }
            else 
            {
                this.lbl.Text = "上传的图片格式不正确:只能上传.png,jpg,.gif,.bmp,.jpeg";
            }
        }

        this.Image1.ImageUrl = this.Request.ApplicationPath + ("Images" + FileUpload1.FileName);//把上传的图片赋给Image1路径


        Update();

    }


    void Update()//把上传图片路径保存到数据库中
    {
        dbhelper.connection.Open();
        int id = Convert.ToInt32(Request.QueryString["Id"]);//获取数据库Logins表中的Id
        string URL = "~/Images/" + this.FileUpload1.FileName;//文件路径名
        string sql = string.Format("UPDATE Logins SET Image='{0}' WHERE Id={1}", URL, id);//SQL语句
        SqlCommand command = new SqlCommand(sql, dbhelper.connection);//定义SQL执行语句
        command.ExecuteNonQuery();//执行SQL语句


        dbhelper.connection.Close();
        command.Dispose();//释放缓存
  
    }


}


猜你喜欢

转载自blog.csdn.net/Lan_Se_Tian_Ma/article/details/80951402
今日推荐