Android Question Upload image to SQL SERVER

bashka_abdyli

Member
Licensed User
Longtime User
Any complete code that shows how to upload an image to SQL Server using HttpUtils with ASP.NET Script on Server ?
 

KMatle

Expert
Licensed User
Longtime User
Who needs Blobs? Just convert it to a Base64 string:

B4X:
Dim FileBuffer(0) As Byte
    FileBuffer=Bit.InputStreamToBytes(File.OpenInput(FilePath,FileName))

    Dim B64String As String
    Dim su As StringUtils
    B64String=su.EncodeBase64(FileBuffer)

In the database just define a column as LONGTEXT which can carry 4 GB (which is more than enough).
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
Who needs Blobs? Just convert it to a Base64 string:

Certain Server configurations will cause errors when using Base64 Encoded strings.

This how I handle Blobs with my ASP.NET page.

B4X:
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim data() As Byte
        data = Request.BinaryRead(Request.TotalBytes)
        Dim DateTaken As Date = ConvertDate(Request.QueryString("datetaken"))
        Dim AssetNo As String = Request.QueryString("AssetNo")
        Dim params(2) As SqlParameter
        params(0) = New SqlParameter("DateTaken", DateTaken)
        params(1) = New SqlParameter("AssetNo", AssetNo)
        params(2) = New SqlParameter("Image", data)

        InsertImage(params)
    End Sub

    Public Sub InsertImage(params() As SqlParameter)
        Dim SQLString As String = "INSERT INTO Images (DateTaken, Image, AssetNo) VALUES (@DateTaken, @Image, @AssetNo)"
        Dim myConnection As New SqlClient.SqlConnection(m_connString)
        ' Create a database command on the connection using query   
        Dim myCommand As New SqlClient.SqlCommand(SQLString, myConnection)
        myCommand.Parameters.AddRange(params)
        myConnection.Open()
        myCommand.ExecuteScalar()
        myConnection.Close()
        myConnection.Dispose()
        myCommand.Dispose()
    End Sub

    Public Function ConvertDate(AndroidDate As Long) As Date
        Dim dt As New Date(1970, 1, 1)
        dt = dt.AddMilliseconds(AndroidDate)
        Return dt
    End Function

And the Android Code.

B4X:
Sub SendImage(Img() As Byte, DateTaken As String, AssetNo As String)
    Dim j As HttpJob
    j.Initialize("I" & AssetNo, Me)
    j.PostBytes(URL & "?datetaken=" & DateTaken & "&AssetNo=" & AssetNo, Img)
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…