ASP.NET store Image in SQL and retrieve for Asp:Image

前端 未结 7 831
挽巷
挽巷 2020-12-01 17:41

I am looking to fileupload a picture jpeg,gif,etc into an SQL database on an updateprofilepicture page. Then on the profile page, I want to retrieve the image from an sql da

7条回答
  •  独厮守ぢ
    2020-12-01 18:15

    As Joel mentioned you should use an HttpHandler or a page to display the image. Here is a sample code to output image (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());
    }
    

    and then use image in your page as :

      
    

提交回复
热议问题