2012-11-29 3 views
0

웹 사이트를 프로그래밍하고 있습니다. 참고 - Visual Studio 2010의 VB에서 프로젝트에 VB를 사용하고 LINQ 데이터베이스를 연결해야합니다. 사용자 등록 페이지가 있습니다. 그들의 이름, 전자 메일, 암호 및 관리자 수준이 필요합니다. 또한 사진을 추가하는 기능입니다. 이것은 표준 asp : fileupload 컨트롤을 통해 구현됩니다. 현재 작동하지 않습니다. 파일을 찾아보고 싶으면 모든 사용자 세부 정보를 "등록"버튼을 클릭하여 데이터베이스에 추가하십시오. 지금까지 가지고있는 코드는 아래와 같습니다. 내가 그것을 추가하려고 할 때마다 나는 오류가 "System.Data.Linq에 converet 형 System.Web.UI.WebControls.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'선언하지 않았습니다. 바이너리 "나는 이것을 피하기 위해 무엇을해야할지 모른다. 내가 사용하고 데이터베이스 테이블은 다음과 같습니다

사용자

-userid (INT, 증가) -Name (NVARCHAR) -Password (NVARCHAR) - 이메일 (NVARCHAR) -PhotoID (이미지)

어떤 제안이 좋을 것입니다. 감사합니다.

답변

1

웹 사이트를 만들 때 이미지를 데이터베이스에 저장하지 않는 것이 좋습니다. 하드웨어에 이미지를 저장하고 이미지 경로 만 데이터베이스로 푸시하는 것이 좋습니다.

이 모범 사례를 몇 가지 설명, 그래서 좀 봐 :

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

+0

안녕 감사합니다.불행하게도, 이것이 특정 하드웨어 위치를 보장 할 수 없기 때문에 uni 서버에서 호스팅되는 대학 프로젝트이므로 나에게 직접적인 문제는 아닙니다. 반면 이미지 (소량)가 데이터베이스에 보관되어 있으면 패키징 할 수 있습니다 모두 함께 호스트하고 – Gavin

+0

파일을 저장하는 유일한 방법이 데이터베이스이고 SQL Server 2008 이상을 사용하는 경우 변수 유형 "이미지"를 지원합니다. 사진을이 유형의 데이터베이스로 저장할 수 있습니다. –

+0

나는 이미 데이터베이스에 선언 된 이미지 필드를 가지고 있지만 실제로 이미지를 업로드하는 방법을 모른다. 언급했듯이 LINQ를 사용하고 있습니다. 문제의 전체 범위는 등록 페이지 (위의 코드)입니다. 사용자는 이름 : - 이름, 이메일, 비밀번호, 관리자 수준을 입력하고 사진을 찾아보고 업로드 할 수 있어야합니다. 하나의 제출 버튼으로 모든 이벤트를 처리하기를 원합니다. FileUpload 컨트롤을 제출 단추에 연결하여 모든 데이터를한데 모아서 한 명의 사용자로 입력하는 방법을 모르겠습니다. – Gavin

0

당신은 마지막으로 당신이 할 필요가 있으므로, SQL 서버와 함께 할하기로 결정하는 경우 :

  1. 이 만들기 이미지의 형식이 될 데이터베이스의 필드
  2. 이 이미지로 모든 매개 변수를 수신하는 저장 프로 시저를 만듭니다.
  3. 응용 프로그램 측면에서 업로드 된 파일을 System.Data.Linq.Binary 유형으로 변환해야 sp에 전달할 수 있으며 다음과 같이 할 수 있습니다. new System.Data.Linq.Binary (FileUpload1. FileBytes)는 데이터베이스에서 이미지를 검색 한 후

, 당신은 같은 개체 유형 System.Data.Linq.Binary을해야합니다. 당신이해야합니다 웹 사이트에 표시하려면 :

  1. 은 SRC에게

    System.Data.Linq.Binary IMG을 IMG을 base64로 문자열

  2. 푸시이 base64로 그것을 변환 = "이미지 데이터베이스에서 검색"

    의 img SRC = "데이터 : 이미지, base64로, Convert.ToBase64String @ (img.ToArray())"답장을

관련 문제