Avatar of K Feening
K Feening
Flag for Australia

asked on 

VB.Net Loade and save image jpg file

Hi I am trying to upload an image - Logo.jpg  display it in an asp image (Image1) then save it to SQL 2014 Server database file 

File name - LogoFile Fields ID - Type = Integer, Logo - Type = Image

Then when program rerun get the image from database and redisplay in the asp image

I have tried a few web for information sites to Save the data but without the Upload showning the jpg I doint know if it works

Folowing is the code for Uploading the logo

then displaying when program is rerun

and Saving the logo

Protected Sub bLogo_Click(sender As Object, e As EventArgs) Handles bLogo.Click
        Dim FolderPath As String = ConfigurationManager.AppSettings("LogoPath")  
If FileUpload1.HasFile Then
            Dim fileDelete = FolderPath & FileUpload1.FileName
            If System.IO.File.Exists(fileDelete) = True Then
                Catch ex As Exception       
                End Try
            End If
            Dim filePath As String = FolderPath & FileUpload1.FileName
            Image1.Attributes.Add("style", "background-repeat: no-repeat; background-image:url(filepath)")   ???
            Image1.ImageUrl = filePath     ??? 
            Exit Sub
        End If
    End Sub

Open in new window

If Not Me.IsPostBack Then
            Dim conn As New SqlConnection(connString)
            Using sqlCommand = New SqlCommand()
                With sqlCommand
                    If conn.State = ConnectionState.Open Then conn.Close()
                    .CommandText = "SELECT Logo FROM LogoFile where Id=@Id"
                    .Parameters.AddWithValue("@Id", 1)
                    .Connection = conn
                    .CommandType = CommandType.Text
                    Dim logo = .ExecuteScalar()

                    Image1.ImageUrl = logo ???????
                End With
            End Using


        End If

Open in new window

Dim conn As New SqlConnection(connString)
Dim SqlCommand = New SqlCommand() With SqlCommand    .CommandText = "Insert Into File (Id, Logo)"
   .CommandText = .CommandText & "  Values (@ID, @Logo)"
   .Connection = conn
   .CommandType = CommandType.Text
   .Parameters.Clear()    .Parameters.AddWithValue("@FD", "FD")    Dim url As String = Image1.ImageUrl
                    Dim bytes As Byte() = Convert.FromBase64String(Image1.ImageUrl.Split(","c)(1))
                    .Parameters.AddWithValue("@Logo", bytes)    .ExecuteNonQuery() end with

Open in new window

Visual Basic.NETImages and PhotosASP

Avatar of undefined
Last Comment
K Feening

8/22/2022 - Mon