【问题标题】:How to add an image to a database table如何将图像添加到数据库表
【发布时间】:2012-11-29 12:07:14
【问题描述】:

我在 Visual Studio 2010 中用 VB 编写网站 Note -Not A Project 并绑定了一个 LINQ 数据库。我构建了一个用户注册页面,提示用户输入他们的姓名、电子邮件、密码和他们需要的管理员级别。这也是添加照片的功能。这是通过标准的 asp:fileupload 控件实现的。目前它不工作。我希望浏览文件,然后单击“注册”按钮将所有用户详细信息添加到数据库中。我到目前为止的代码如下。

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If IsPostBack Then
        Dim UpPath As String
        UpPath = "~/Uploads/"
        Image1.Visible = True
        Image1.ImageUrl = Session("ImagePath")

        If Not Directory.Exists(UpPath) Then
            Directory.CreateDirectory("C:\UploadedUserFiles\")
        End If

    End If
End Sub

Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click


    Dim savePath = "\Uploads\"
    Dim appPath As String = Server.MapPath("~")

    If (FileUPload1.HasFile) Then
        Dim savePath2 As String = savePath & Session("currentuser") & "" & FileUPload1.FileName
        FileUPload1.SaveAs(appPath & savePath2)
        Session("ImagePath") = "." & savePath
    End If


    ' variables to store the user's registration details
    Dim username As String
    Dim email As String
    Dim password As String
    Dim retypedPassword As String

    ' variables to store the user's selected roles
    Dim productOwner As Boolean
    Dim projectManager As Boolean
    Dim scrumMaster As Boolean
    Dim developer As Boolean

    ' populate variables with the values from the web page
    username = txtUsername.Text
    email = txtEmail.Text
    password = txtPassword.Text
    retypedPassword = txtRetypePassword.Text

    ' check which user roles have been selected
    productOwner = checkProductOwner.Checked
    projectManager = checkProjectManager.Checked
    scrumMaster = checkScrumMaster.Checked
    developer = checkDeveloper.Checked


    ' boolean to check if the entered details are valid
    Dim isValid As Boolean
    isValid = True

    ' check if the values entered by the user are valid
    If _
        String.IsNullOrEmpty(username) Or _
        String.IsNullOrEmpty(email) Or _
        String.IsNullOrEmpty(password) Or _
        String.IsNullOrEmpty(retypedPassword) Or _
        password <> retypedPassword Then
        isValid = False
    End If

    ' if the values are valid, then populate the USER table with the new user and the USER ROLES table
    ' with the roles they are allowed in any project that will be created

    If isValid Then
        ' set up LINQ connection with database
        Dim db As New AgileClassesDataContext()

        ' create a user to populate a row in the USER table
        Dim user As New User With _
        {.Name = username, _
         .Password = password, _
         .Email = email}

        ' add the new user to the USER table
        db.Users.InsertOnSubmit(user)

        ' submit the changes to the database
        Try
            db.SubmitChanges()
        Catch ex As Exception
            Console.WriteLine(ex)
            db.SubmitChanges()
        End Try

目前我没有在数据库中声明“FileUPload1”,因为每当我尝试添加它时,我都会收到一条错误消息“无法将 System.Web.UI.WebControls.FileUpload 类型转换为 System.Data.Linq.Binary”我不不知道该怎么做才能避免这种情况。我使用的数据库表是:

用户

-UserID(整数,递增) -名称(nvarchar) -密码(nvarchar) -电子邮件(nvarchar) -PhotoID(图片)

任何建议都会很棒。谢谢

【问题讨论】:

    标签: database vb.net image linq web


    【解决方案1】:

    我建议您在构建网站时不要将图像保存到数据库中。 最好将图片保存在硬件上,只推送图片路径到数据库。

    有一些最佳实践的解释,所以看看:

    https://stackoverflow.com/a/348373/350977

    【讨论】:

    • 您好,感谢您的回复。不幸的是,这对我来说不是直截了当,因为这是一个将托管在 uni 服务器上的大学项目,所以我不能保证特定的硬件位置,而如果图像(少量)保存在数据库中,我可以打包它一起来主持它
    • 如果您保存文件的唯一方式是数据库并且您使用的是 SQL Server 2008 或更高版本,它支持变量类型“图像”。您可以将图片保存为数据库中的这种类型。
    • 我的数据库中已经声明了图像字段,但我不知道如何实际将图像上传到其中。如前所述,我正在使用 LINQ。问题的全部范围是我有一个注册页面(上面的代码)。用户需要能够输入他们的:-姓名、电子邮件、密码、管理员级别并浏览并上传图片。我希望所有事件都由一个提交按钮处理。我不知道如何将文件上传控件绑定到提交按钮以整理所有数据并作为一个用户输入
    【解决方案2】:

    如果您最终决定使用 SQL Server,那么您需要:

    1. 在数据库中创建一个图像类型的字段
    2. 创建一个存储过程,该过程将使用此图像接收您的所有参数
    3. 在您的应用程序端,您需要将上传的文件转换为 System.Data.Linq.Binary 类型以将其传递给 sp,您可以这样做:new System .Data.Linq.Binary(FileUpload1.FileBytes)

    从数据库中检索图像后,您将拥有相同的对象类型 System.Data.Linq.Binary。要在您的网站上显示,您必须:

    1. 转成base64字符串
    2. 把这个base64推到img src

      System.Data.Linq.Binary img = "从您的数据库中检索到的图像"

      img src="data:image;base64,@Convert.ToBase64String(img.ToArray())"

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2014-01-29
      • 2020-03-25
      • 2012-07-04
      • 2015-05-12
      • 1970-01-01
      相关资源
      最近更新 更多