Hi guys, i have an image, i need to send it to the webservice with json.
How can i convert the memory block to text?
dim xpicture as iOSImage
xpicture=ImageView1.image
dim idat as Text
PictureView.RSocket.ClearRequestHeaders
dim bd as new BinaryStream(xpicture.ToData("public.jpeg"))
if bd <> NIL then
idat = bd.Read(bd.length)
bd.close
end
idat="""+idat+"""
Dim requestContent As Text ="{""ID"":3562, ""Image1"":"+idat+"}" //Type error
If Not requestContent.Empty Then
Dim data As Xojo.Core.MemoryBlock
data = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(requestContent)
RSocket.SetRequestContent(data, "application/x-www-form-urlencoded")
End If
PictureView.RSocket.Send("POST", "http://192.168.5.48:81/special/ImageUpload")
[code]Dim saveFile As FolderItem = SpecialFolder.Documents.Child(“tempimage.jpeg”)
ImageView1.Image.WriteToFile(saveFile,“public.jpeg”)
Dim f As FolderItem = SpecialFolder.Documents.Child(“tempimage.jpeg”)
Dim b As BinaryStream
b = BinaryStream.Open(f, BinaryStream.LockModes.Read)
Dim mb As New MemoryBlock(b.Read(b.Length))
b.Close
'Dim image As iOSImage
'image = Image.FromData(mb)
'ImageView1.Image = image
dim kepkod as Text
kepkod=Xojo.Core.TextEncoding.UTF8.ConvertDataToText(mb)
kepkod=""""+kepkod+""""
Dim sendMethod As Text = “POST”
Dim requestContent As Text ="{"“Azonosito”":3562, ““Kep””:"+kepkod+"}"
If Not requestContent.Empty Then
Dim data As Xojo.Core.MemoryBlock
data = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(requestContent)
RSocket.SetRequestContent(data, “application/x-www-form-urlencoded”)
End If
You probably cannot convert a picture data to UTF8 text, as it contains numerous non printable, invalid characters. That is probably the error you are getting.
You should probably read the content of the memoryblock byte per byte and turn each into hex to build a Text.
[quote=276556:@Roland Maszlag]Thank you Michel, will give it a try.
On the other hand, I’m looking for a solution to upload image files to ftp server from IOS client.[/quote]
I never tried to ftp in Xojo, but httpsocket works very nicely to upload files. That means you will have to use a client on the server side.
[quote=276556:@Roland Maszlag]Thank you Michel, will give it a try.
On the other hand, I’m looking for a solution to upload image files to ftp server from IOS client.[/quote]
Even so, the data should not be Text. Text implies that the contents have an encoding.
I tried to convert the bytes of image to hex and put them into a text variable. It takes too long to convert them, so i can’t use it.
dim lastByteIndex as integer = mb.Size - 1
for i as integer = 0 to lastByteIndex
kepkod=kepkod+mb.data.Byte(i).ToHex(4)
next
the webservice code i would use to upload files into mysql blob:
[code] Dim json As New JSONItem(jsonData)
Dim json2 As New JSONItem(jsonKep)
Dim id As Integer = json.Value(“Azonosito”)
Dim kep as String=json2.Value(“Kep”)
//convert ‘kep’ back to binarystream?
Dim sql As String = “UPDATE Aru_nev SET Image1=”+kep+" WHERE ID="+str(id)+""
Dim ps As MySQLPreparedStatement = DB2.Prepare(sql)
db2.SQLExecute(sql)[/code]
I just can’t convert the picture into a sendable format.
Jason code works fine, converting the picture to data works fine, but the hurdle is in converting the data to text.
[code] dim mb as MemoryBlock = pic_1.ToData(“public.png”)
dim letexte as Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(mb)
dim limage as text = encodeBase64_2(letexte)[/code]
The third line triggers a runtime Exception “The data could not be converted to text with this encoding.”
Jason, looking at your method I wonder if it could not be fed directly with a MemoryBlock instead of Text :
[code] Function encodeBase64(aText as Text) As Text
declare function dataUsingEncoding lib FoundationLib selector “dataUsingEncoding:” (obj_id as ptr, encoding as Integer) as ptr
declare function stringWithString lib FoundationLib selector “stringWithString:” (obj_id as ptr, str as CFStringRef) as ptr
declare function NSClassFromString lib FoundationLib (clsName as CFStringRef) as ptr
const NSUTF8StringEncoding = 4
dim str as Ptr = stringWithString(NSClassFromString(“NSString”), aText)
dim mData as ptr = dataUsingEncoding(str,NSUTF8StringEncoding)
declare function base64EncodedStringWithOptions lib FoundationLib selector “base64EncodedStringWithOptions:” _
(obj_id as ptr, options as Integer) as CFStringRef
Return base64EncodedStringWithOptions(mData,1)
End Function[/code]
I am not as competent as you are, but would it not be possible to set mData with the MemoryBlock instead of having to convert aText in the first place ?
Somehow it was clearer for me from Phillip Zedalis method :
[code]Public Function encodeBase64(extends mb as MemoryBlock) as Text
'From Phillip Zedalis
’ Modified by Michel Bujardet
'Function EncodeBase64(Extends MB as MemoryBlock) As Text
Declare Function dataWithBytes Lib “UIKit” Selector “dataWithBytes:length:” (class_id As Ptr, bytes As Ptr, length As UInt32) As Ptr
Declare Function base64EncodedStringWithOptions Lib “UIKit” Selector “base64EncodedStringWithOptions:” (class_id As Ptr, options As UInt32) As CFStringRef
// Create NSData pointer to be point of reference.
Dim data As Ptr
data = NSClassFromString(“NSData”)
// Create NSData object using MemoryBlock
Dim dataRef as Ptr = dataWithBytes(data, mb.Data, mb.Size)
// Create Text object to hold Base64 encoded string.
Dim x As Text
x = base64EncodedStringWithOptions(dataRef, 0)
// Return Base64 encoded string
Return x
'End Function
End Function
[/code]
Instead of converting text in memory block first, I feed directly the memoryBlock to dataRef. Now no more textencoding error
EncodeHex is for string and thus does not work on iOS. The only way to do hex is byte-by-byte. It’s not very difficult, but any feature I have to put into nearly every project I touch really feels like it should be built in.
Base64 is less urgent, but still valuable given the complexity and misinformation. Look how many messages in this thread were required to find an implementation that works.
And yes, given the glacial speed of new framework progress, 2029 even seems optimistic.