Android Question Problem to Upload file with Web API Rest

Yayou49

Active Member
Licensed User
(B4A 5.80)

Hi,

I'm trying to implement a file upload from my device to a Web API.
I've tried to do it as shown in many examples.
Hereafter my code:
B4X:
Dim out As OutputStream
out.InitializeToBytesArray(0)
Dim In As InputStream = File.OpenInput(File.DirInternal, "1.jpg")
File.Copy2(In, out)
      
Dim job1 As HttpJob
job1.Initialize("Photo", "Photo")
job1.PostBytes("http:....../File", out.ToBytesArray)
job1.GetRequest.SetHeader("Authorization",MyToken)
job1.GetRequest.SetContentType("application/json")
job1.GetRequest.Timeout = 60000

On my .net side here is my controller's code:
B4X:
<[POST]("file")>
<HttpPost()>
     Public Function Send(<FromBody()> WebGed As Byte()) As Boolean

            'code to store the file in data base

            Return True
      End Function

My problem is: NOTHING in my WebGed .....

Any ideas ???

Thanks in advance.
 

Yayou49

Active Member
Licensed User
Well, I found a tricky solution to solve my problem :eek:
(Thanks to my friend John for his help :))
I copy my solution if it can helps someone ;)

Well, first step, I've modified the first part in B4A for my needs.
I just create a Map because I need one information more on my WS side:

B4X:
    Dim Myout As OutputStream
    Myout.InitializeToBytesArray(0)
    Myout = File.OpenOutput(File.DirInternal, "/1.jpg", False)
    Myout.WriteBytes(Data, 0, Data.Length)
    Myout.Close   
   
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Dim In As InputStream = File.OpenInput(File.DirInternal, "/1.jpg")
    File.Copy2(In, out)
   
    Dim Input As Map
    Input.Initialize
    Input.Put("IdRma",OnSiteMain.IdRMA)
    Input.Put("Fic",out.ToBytesArray)
   
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Input)
   
    Log(JSONGenerator.ToPrettyString(2))
   
    Dim Job1 As HttpJob
    Job1.Initialize("Photo", "Photo")
    Job1.PostString(Main.URL & "tools/file/", JSONGenerator.ToPrettyString(2))
    Job1.GetRequest.SetHeader("Authorization",Main.MyToken)
    Job1.GetRequest.SetContentType("application/json")
    Job1.GetRequest.Timeout = 60000

And now on my WS side:

With WebGed define as:

B4X:
Public Class WebGed

    Public Property IdRma As Integer
    Public Property Fic As New List(Of SByte)

End Class

B4X:
        <[POST]("file")>
        <HttpPost()>
        Public Function Send(<FromBody()> WebGed As WebGed) As Boolean

          
            Dim Dest(Fic.Fic.ToArray.Length) As Byte
            System.Buffer.BlockCopy(Fic.Fic.ToArray, 0, Dest, 0, Fic.Fic.ToArray.Length)

            Dim MyMemoryStream as System.IO.MemoryStream = New System.IO.MemoryStream(Dest)
        
            'following code to store file in database


            Return True
        End Function


And everything is working fine !!!!!!
 
Upvote 0

Yayou49

Active Member
Licensed User
Just one thing, system doen't support big file.
But with .ResizePicture library, you can shrink your picture.
 
Upvote 0
Top