gpt4 book ai didi

ASP.NET 在 SQL 中存储图像并检索 Asp :Image

转载 作者:行者123 更新时间:2023-12-01 07:23:02 25 4
gpt4 key购买 nike

我希望在 updateprofilepicture 页面上将图片 jpeg、gif 等文件上传到 SQL 数据库中。然后在配置文件页面上,我想从 sql 数据库中检索图像并将其显示在 Asp:Image 控件中。我有很多代码试图做到这一点,但它不起作用。该表包含一个图像类型的列。

最佳答案

正如 Joel 提到的,您应该使用 HttpHandler 或页面来显示图像。这是输出图像的示例代码( Image.ashx ):

// ProcessRequest method of Image.ashx
long imageId = Convert.ToInt64(Request.QueryString["ImageId"]);

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
"SELECT ImageFile FROM ImageTable WHERE ImageId = @ImageID", conn))
{
command.Parameters.Add("@ImageID", SqlDbType.Int).Value = imageId;
conn.Open();

Response.ContentType = "image/gif";
Response.BinaryWrite((byte[]) command.ExecuteScalar());
}

然后在您的页面中使用图像作为:
  <asp:Image id="Image1" runat="server" ImageUrl="Image.ashx?ImageID=12"/>

关于ASP.NET 在 SQL 中存储图像并检索 Asp :Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/698912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com