0

Webサイトをプログラミングしています注-VisualStudio2010の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、incrementing)-Name(nvarchar)-Password(nvarchar)-Email(nvarchar)-PhotoID(image)

どんな提案も素晴らしいでしょう。ありがとう

4

2 に答える 2

1

Web サイトを構築するときは、画像をデータベースに保存しないことをお勧めします。イメージをハードウェアに保存し、イメージ パスのみをデータベースにプッシュすることをお勧めします。

ベスト プラクティスの説明がいくつかあるので、以下を参照してください。

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

于 2012-11-29T13:00:33.230 に答える
0

最終的に SQL Server で行うことにした場合は、次のことを行う必要があります。

  1. 画像のタイプになるデータベースにフィールドを作成します
  2. このイメージですべてのパラメーターを受け取るストアド プロシージャを作成します。
  3. アプリケーション側では、アップロードしたファイルを sp に渡すためにSystem.Data.Linq.Binaryタイプに変換する必要があります。次のように行うことができます。

データベースからイメージを取得すると、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 に答える