如何从名称存储在数据库中的文件夹中检索图像?

时间:2016-12-14 18:51:50

标签: asp.net webforms

protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-Q02V0BS\AQ;Initial Catalog=upload_file;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select filename from file_table",con);
        SqlDataReader r = cmd.ExecuteReader();
        while (r.Read())
        {
            Response.Write("<img src=&quot;" + Server.MapPath("~/Images/")+ r[0].ToString()+"&quot;/>");
        }
        con.Close();
    }

我已将图片名称保存到file_table且列名为filename的表格中。单击此按钮时,不会使用当前图像路径生成图像。

1 个答案:

答案 0 :(得分:0)

尝试创建图像对象,然后修改其属性。在我的例子中,我创建了一个div,我可以将图像添加到。

demo.aspx:

<div id="imageContainer" runat="server"></div>

demo.aspx.cs

HtmlImage myImage = new HtmlImage();
string myImagePath;

myImagePath = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
myImage.Src = myImagePath;

imageContainer.Controls.Add(myImage);