1

我想将图像上传到我网站文件夹中的文件夹,然后将其路径保存到数据库并在控制图像中显示上传的图像。当我执行我的页面时,图像已成功上传并保存到数据库,但它不显示在图像控件上。

这是我的观点:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:Image ID="Image1" runat="server" Height="91px" Width="145px" />
    </form>

    <p>
        &nbsp;</p>

</body>
</html>

这是我的代码:

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

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim cn As SqlConnection

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FileUpload1.PostedFile IsNot Nothing Then
            Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)

            'Save files to disk
            FileUpload1.SaveAs(Server.MapPath("~/im/" & FileName))

            'Add Entry to DataBase

            Dim strQuery As String = "insert into dbo.images" & " values(@FileName, @FilePath)"
            Dim cmd As New SqlCommand(strQuery)
            cmd.Parameters.AddWithValue("@FileName", FileName)
            cmd.Parameters.AddWithValue("@FilePath", "~/im/" & FileName)
            cmd.CommandType = CommandType.Text
            cmd.Connection = cn

            Try
                cn.Open()
                cmd.ExecuteNonQuery()

            Catch ex As Exception
                Response.Write(ex.Message)
            Finally

                cn.Close()
                cn.Dispose()
            End Try
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn = New SqlConnection("server=SEVO-PC;initial catalog=controle;integrated security=true")
    End Sub

    Protected Sub FileUpload1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileUpload1.Load
        Image1.ImageUrl = "im/" & FileUpload1.FileName

    End Sub
End Class
4

1 回答 1

0

尝试以下编辑

Protected Sub FileUpload1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileUpload1.Load
        Image1.ImageUrl = Server.MapPath("/im/") & FileUpload1.FileName

    End Sub
于 2013-05-13T18:02:06.580 回答