小编典典

从SQL数据表到ASP的图像文件:图像控件

sql

我在从SQL读取图像并在asp:Image控件中可视化时遇到一些问题。该方案是:

  1. 从本地计算机上传图片

  2. 网站将图像文件转换为位数组并保存

  3. 然后从表中读取它,并将其解析为特定的asp:Image

我有以下代码:

<asp:Content ID="wrapperContent" ContentPlaceHolderID="wrapper" Runat="Server">

    <asp:Image ID="profileImage" runat="server" ImageUrl="<%=img %>" /><br />

    <asp:Button ID="ChangeImage" runat="server" Text="Change Photo" OnClick="ChangeImage_Click" />

    <asp:FileUpload ID="FileUpload" runat="server" Visible="false" />

    <asp:Button ID="UploadImage" runat="server" Text="Upload Photo" visible="false" OnClick="UploadImage_Click"/>

</asp:Content>

后面的代码:

public partial class Details : System.Web.UI.Page
{
    public int id;    
    public Bitmap bitmap;
    public string imgUrl;


    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["userName"] != null)
        {
            string sql = "select * from users where username='" + Session["userName"]+"'";
            SqlDataReader sdr = operateData.getRow(sql);
            sdr.Read();
            id = Int32.Parse(sdr["Id"].ToString());

            sql = "select * from profiles where userId='" + id+"'";  
            SqlDataReader sdrPr = operateData.getRow(sql);
            sdrPr.Read();

        SqlConnection con = operateData.createCon();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DataRow row = dt.Rows[0];
            byte[] imgBytes = (byte[])row["img"];
            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

            string filePath = Server.MapPath("temp") + "//" + "img" + DateTime.Now.Ticks.ToString() + ".png";
            FileStream fs = File.Create(filePath);
            fs.Write(imgBytes, 0, imgBytes.Length);
            fs.Flush();
            fs.Close();

            profileImage.ImageUrl = filePath;

        }
        else
        {
            Response.Redirect("Login.aspx");
        }       
    }
    protected void ChangeImage_Click(object sender, EventArgs e)
    {
        FileUpload.Visible = true;
        UploadImage.Visible = true;
    }
    protected void UploadImage_Click(object sender, EventArgs e)
    {
        if (FileUpload.HasFile)
        {
        string sql = "update profiles set img='" + img+"' where userId='" + id + "'";
            operateData.execSql(sql);            
        }
    }
}

我想念什么..?我该如何管理?


阅读 173

收藏
2021-04-22

共1个答案

小编典典

请尝试以下代码。 在Web应用程序中,图像控件需要图像相对路径,因此您必须按如下所示更改图像url

profileImage.ImageUrl =“ img” + DateTime.Now.Ticks.ToString()+“ .png”;

protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["userName"] != null)
            {
                string sql = "select * from users where username='" + Session["userName"]+"'";
                SqlDataReader sdr = operateData.getRow(sql);
                sdr.Read();
                id = Int32.Parse(sdr["Id"].ToString());

                sql = "select * from profiles where userId='" + id+"'";  
                SqlDataReader sdrPr = operateData.getRow(sql);
                sdrPr.Read();

            SqlConnection con = operateData.createCon();
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                DataRow row = dt.Rows[0];
                byte[] imgBytes = (byte[])row["img"];
                System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

                string filePath = Server.MapPath("temp") + "//" + "img" + DateTime.Now.Ticks.ToString() + ".png";
                FileStream fs = File.Create(filePath);
                fs.Write(imgBytes, 0, imgBytes.Length);
                fs.Flush();
                fs.Close();

                profileImage.ImageUrl = "img" + DateTime.Now.Ticks.ToString() + ".png";

            }
            else
            {
                Response.Redirect("Login.aspx");
            }       
        }
2021-04-22