locked
Looking for simple vb.net code to upload and image from client to web form image control and save it to sql express database RRS feed

  • Question

  • User-453569901 posted

    I hoping to find a current and  simple way to do this. 

    I'm using VWD Express 2008 and SQL Express 2008.

    I'm able to get the client's path to the image file using an asp:FileUpload control on the web form.

    How do I upload the client's image file and display it in the web form's image control?

    How do I save it to the sqlserver? The image table is already set up with a "Image" data type column. Can the image be saved to the database as simply as other data types?

     

    Saturday, January 16, 2010 8:40 PM

Answers

  • User-1266694110 posted

    I think you should save image path to database and your image will upload to Images folder. Please try this:

    - Images path in Database like this: ~/Images/myimage.jpg

    - Then you upload your image to Images folder using FileUpload control:

    Public Shared Function PostImageFile(ByVal RootPath As String, ByVal Upload As FileUpload) As Boolean
            If Upload.HasFile = True Then
                Dim fileName As String = Upload.FileName()
                Dim extension As String = System.IO.Path.GetExtension(fileName).ToLower()
                If (extension = ".gif") Or (extension = ".jpg") Or (extension = ".jpeg") Or (extension = ".png") Then
                    Dim fileSize As Integer = Upload.PostedFile.ContentLength
                    ' Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
                    If (fileSize < 2100000) Then
                        Dim myPhysPath As String = Current.Server.MapPath(RootPath) & "\" & fileName
                        Upload.SaveAs(myPhysPath)
                        Return True
                    End If
                End If
            End If
            Return False
        End Function

    Hope this help !

    Visit http://www.vemaybaygiare.net/ to take a VietNam travel

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 16, 2010 10:03 PM
  • User1006193418 posted

    HttpContext.Current.Server.MapPath(RootPath) & "\" & fileName

    Hi,

    I suggest you can use Path.Combine method for the image's path instead of join them via &.

    Dim myPhysPath As String = System.IO.Path.Combine(HttpContext.Current.Server.MapPath(RootPath), fileName)

    For more information about this method, please refer to this link: http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2010 1:30 AM

All replies

  • User-1266694110 posted

    I think you should save image path to database and your image will upload to Images folder. Please try this:

    - Images path in Database like this: ~/Images/myimage.jpg

    - Then you upload your image to Images folder using FileUpload control:

    Public Shared Function PostImageFile(ByVal RootPath As String, ByVal Upload As FileUpload) As Boolean
            If Upload.HasFile = True Then
                Dim fileName As String = Upload.FileName()
                Dim extension As String = System.IO.Path.GetExtension(fileName).ToLower()
                If (extension = ".gif") Or (extension = ".jpg") Or (extension = ".jpeg") Or (extension = ".png") Then
                    Dim fileSize As Integer = Upload.PostedFile.ContentLength
                    ' Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
                    If (fileSize < 2100000) Then
                        Dim myPhysPath As String = Current.Server.MapPath(RootPath) & "\" & fileName
                        Upload.SaveAs(myPhysPath)
                        Return True
                    End If
                End If
            End If
            Return False
        End Function

    Hope this help !

    Visit http://www.vemaybaygiare.net/ to take a VietNam travel

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 16, 2010 10:03 PM
  • User-453569901 posted

    Thank you. That's just what I was hoping for.

    One change:

     Dim myPhysPath As String = Current.Server.MapPath(RootPath) & "\" & fileName

    should be

     Dim myPhysPath As String = HttpContext.Current.Server.MapPath(RootPath) & "\" & fileName

     

    Sunday, January 17, 2010 10:15 AM
  • User1006193418 posted

    HttpContext.Current.Server.MapPath(RootPath) & "\" & fileName

    Hi,

    I suggest you can use Path.Combine method for the image's path instead of join them via &.

    Dim myPhysPath As String = System.IO.Path.Combine(HttpContext.Current.Server.MapPath(RootPath), fileName)

    For more information about this method, please refer to this link: http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2010 1:30 AM