从db显示(不下载)图像

时间:2013-12-17 19:14:36

标签: asp.net vb.net image

我按照http://www.aspsnippets.com/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx上的代码设法将图像上传到我的数据库。

但是当我按照http://www.aspsnippets.com/articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx上的代码时,图像被下载而不是显示。


将显示图像的页面的代码:

<%@ 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

将图像上传到数据库很好。

问题在于从数据库显示图像,图像被下载而不是显示。

3 个答案:

答案 0 :(得分:0)

您是将图像作为BLOB或VARBINARY存储在数据库中,还是存储数据库中图像的路径?

如果您要存储路径,则只需设置ImageURL控件的<asp:Image>属性:

Image1.ImageURL = [column in database];

如果将图像作为VARBINARY存储在数据库中,请参阅以下答案: https://stackoverflow.com/a/8084628/117658

答案 1 :(得分:0)

尝试写入输出流(注意不是100%确定VB.NET语法):

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

答案 2 :(得分:0)

尝试了大卫的建议“尝试删除”附件;“来自内容处置标题。”现在显示图像。但与此同时,另一个问题浮出水面,将在另一个问题上解决。