1

我也知道这个(某种)问题已经被问过并回答了很多次。我自己曾经问过这个问题。但我还没有成功。如何在特定员工 ID 的图像控件中显示图像...这是我的代码:

`bind() GridView1.Visible = "True"

    Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("ImageID"))
    Dim dt As DataTable = GetData(cmd)
    If dt IsNot Nothing Then
        **Dim bytes() As Byte = CType(dt.Rows(1)("Image"), 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(1)("Name").ToString())
        Response.BinaryWrite(bytes)
        Response.Flush()
        Response.End()
    End If` 

在粗体行出现错误。“位置 1 没有行”...实际上我想获取(显示)特定员工 ID 的图像。它与行位置有何关系?我不知道我为什么要写那行代码。无论如何,我知道有些人可以帮助我,所以我提前感谢他们......

好的,这是我将图像添加到数据库的代码。这工作正常。

 Dim imageData As Byte() = New Byte(FileUpload1.FileContent.Length) {}
    FileUpload1.FileContent.Read(imageData, 0, Convert.ToInt32(FileUpload1.FileContent.Length))

    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString)
    con.Open()
    If EmployeeIDTextBox.Text = "" Then
        MsgBox("Please Enter EmployeeID to Add Photo")
    Else
        Dim com As New SqlCommand("UPDATE EmployeeTable SET IMAGE = @IM where EmployeeID='" & EmployeeIDTextBox.Text & "'", con)
        Dim filePath As String = Server.MapPath(FileUpload1.FileName)
        'Dim imageData As Byte() = File.ReadAllBytes(filePath)
        com.Parameters.AddWithValue("@IM", imageData)
        com.ExecuteNonQuery()
        MsgBox("File Saved Successfully!")
    End If
End Sub

现在这是我在图像控件中检索和显示相同图像的代码......

  bind()
    GridView1.Visible = "True"
    'If Request.QueryString("ImageID") IsNot Nothing Then
    Dim strQuery As String = "SELECT Image FROM EmployeeTable WHERE EmployeeID =@EmployeeID"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)
    cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value() = Convert.ToInt32(Request.QueryString("Image"))
    Dim dt As DataTable = GetData(cmd)
    If dt IsNot Nothing Then
        Dim bytes() As Byte = CType(dt.Rows(0)("Image"), 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(1)("Name").ToString())
        Response.BinaryWrite(bytes)
        Response.Flush()
        Response.End()
    End IF

这不工作...

4

2 回答 2

0

你可以使用 Http Handler 并使用下面的代码ProcessRequest()

int Id = Convert.ToInt32(Request.QueryString["ImgId"]);
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
"SELECT ImageFile FROM ImageTable WHERE ImageId = @ImgId", conn))
{
    command.Parameters.Add("@ImgId", SqlDbType.Int).Value = Id ;
conn.Open();
Response.ContentType = "image/gif"; //u can set it per ur requirement
Response.BinaryWrite((byte[]) command.ExecuteScalar());
}

然后在您的页面中使用图像:

  <asp:Image id="Img_Db" runat="server" ImageUrl="Image.ashx?ImgId=1"/>
于 2013-03-04T06:46:09.090 回答
0

数组索引从 0 开始

利用

Dim bytes() As Byte = CType(dt.Rows(0)("Image"), Byte())

你怎么不知道你为什么写这个?

您拥有的代码将提示用户将图像文件保存在他的电脑上。
它不会在图像控件中显示图像。

于 2013-03-04T04:39:59.427 回答