0

I followed the codes on http://www.aspsnippets.com/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx and managed to upload images onto my db.

But when i followed the codes on http://www.aspsnippets.com/articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx the images got downloaded instead of being displayed.


code for the page that will display the image:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="imgTest1.aspx.vb" Inherits="imgTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Image ID="Image1" runat="server" ImageUrl="imgTest1.aspx?id=1" />
        </div>
    </form>
</body>
</html>



Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Partial Class imgTest
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Request.QueryString("id") IsNot Nothing Then

            Dim strQuery As String = "select name, contentType, data from [imageTest] where id=1"
            Dim cmd As SqlCommand = New SqlCommand(strQuery)
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32("1")

            Dim dt As DataTable = GetData(cmd)

            If dt IsNot Nothing Then
                Dim bytes() As Byte = CType(dt.Rows(0)("data"), Byte())
                Response.Buffer = True
                Response.Charset = ""
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.ContentType = dt.Rows(0)("ContentType").ToString()
                Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows(0)("name").ToString())
                Response.BinaryWrite(bytes)
                Response.Flush()
                Response.End()
            End If

        End If

    End Sub

    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("CapstoneConnectionString1").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Response.Write(ex.Message)
            Return Nothing
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function

End Class

The uploading of image onto the db is fine.

The problem lies with the displaying of the image from the db, the image gets downloaded instead of being displayed.

j_t_fusion
  • 223
  • 2
  • 7
  • 20
  • 1
    Try removing "attachment;" from the content-disposition header. – David Dec 17 '13 at 19:17
  • 1
    An HTTP handler is a more efficient way to serve the images http://support.microsoft.com/kb/307997 – geedubb Dec 17 '13 at 19:19
  • @David Tried your suggestion and now the image is displaying. however the page is only displaying the image, all other labels, texts, etc. that are supposed to be on the page are not there. Oh well... time to create another question. – j_t_fusion Dec 17 '13 at 20:54

3 Answers3

1

Try writing to outputstream instead(note not 100% sure of the VB.NET syntax):

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Partial Class imgTest
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Request.QueryString("id") IsNot Nothing Then

            Dim strQuery As String = "select name, contentType, data from [imageTest] where id=1"
            Dim cmd As SqlCommand = New SqlCommand(strQuery)
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32("1")

            Dim dt As DataTable = GetData(cmd)

            If dt IsNot Nothing Then
                Dim bytes() As Byte = CType(dt.Rows(0)("data"), Byte())
                Response.Buffer = True
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.ContentType = dt.Rows(0)("ContentType").ToString()
                Response.OutputStream.Write(bytes(), 0, bytes().Length);
                Response.Flush()
                Response.End()
            End If

        End If

    End Sub

    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("CapstoneConnectionString1").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Response.Write(ex.Message)
            Return Nothing
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    End Function

End Class
geedubb
  • 4,048
  • 4
  • 27
  • 38
1

Tried David's suggestion of "Try removing "attachment;" from the content-disposition header." and the image is now displaying. But at the same time, another issue has surfaced, which will be tackled in another question.

j_t_fusion
  • 223
  • 2
  • 7
  • 20
0

Are you storing the image in your database as a BLOB or VARBINARY or are you storing the path to the image in the database?

If you're storing the path then you would just set the ImageURL property of the <asp:Image> control:

Image1.ImageURL = [column in database];

If storing the image in a database as a VARBINARY, see this answer: https://stackoverflow.com/a/8084628/117658

Community
  • 1
  • 1
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117