0

我正在编写一个网站注释 -在 Visual Studio 2010 中的 VB 中不是一个项目,并绑定了一个 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 (int, 递增) -Name (nvarchar) -Password (nvarchar) -Email (nvarchar) -PhotoID (image)

任何建议都会很棒。谢谢

4

2 回答 2

1

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

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

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

于 2012-11-29T13:00:33.230 回答
0

如果您最终决定使用 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())"

于 2012-11-30T08:34:17.760 回答