Image file from SQL data table in asp: image management

I have some problems reading images from SQL and rendering in asp: Image control. Scenario:

  • Download image from local computer

  • Website turns image file into bitmap and saves it

  • Then read it from the table and parse it to a specific asp: Image

I have the following code:

<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>

And the code behind:

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);            
        }
    }
}

What will I miss ..? how can i manage it?

0
source share
2 answers

, .   - ,   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");
            }       
        }
0

Source: https://habr.com/ru/post/1610880/


All Articles