imagedatei von SQL-datatabelle nach asp: imagekontrolle

Ich habe ein paar Probleme mit dem Lesen von imageern aus SQL und visualisieren in asp: imagekontrolle. Das Szenario ist:

  1. Laden Sie das image vom lokalen Computer hoch

  2. Website verwandelt imagedatei in Bit-Array und speichert sie

  3. Dann lesen Sie es aus der Tabelle und parsing Sie es zu bestimmten Asp: Image

Ich habe den folgenden 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> 

Und Code dahinter:

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

Was vermisse ich ..? Wie kann ich es schaffen?

Solutions Collecting From Web of "imagedatei von SQL-datatabelle nach asp: imagekontrolle"

Bitte versuchen Sie den folgenden Code. In der Webanwendung benötigt die imagesteuerung einen relativen Pfad für das image, so dass Sie die image-URL wie folgt ändern müssen

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