Android Question Problem sending file to a Web Service

Yayou49

Active Member
Licensed User
Hi,

B4A V7.8

I'm facing an issue to send a file from B4A to a web service and I don't know if this problem occur on B4A side or on WS side.

Here is my B4A code:


B4X:
    Dim Out2 As OutputStream
   Out2.InitializeToBytesArray(0)
   Dim In As InputStream = File.OpenInput(File.DirRootExternal, "photo.jpg")
   File.Copy2(In, Out2)
   
   Dim Input As Map
   Input.Initialize
   Input.Put("Fic",Out2.ToBytesArray)


   Dim JSONGenerator As JSONGenerator
   JSONGenerator.Initialize(Input)
   
   Dim Job1 As HttpJob
   Job1.Initialize("Photo", "Phototest")
   Job1.PostString(Main.URL & "tools/file", JSONGenerator.ToPrettyString(2))
   Job1.GetRequest.SetHeader("Authorization",Main.MyToken)
   Job1.GetRequest.SetContentType("application/json")
   Job1.GetRequest.Timeout = 600000

On my VB.net web service side, code is also very simple:

B4X:
  <[POST]("file")>
        <HttpPost()>
        Public Function Send(<FromBody()> Web As List(Of SByte)) As Boolean

            'code to store the file in data base
           '.....

            Return True
        End Function

If my jpg is a small one (200kb) this code is successfull.
If I try the same with a picture with size = 3.5Mb it fails.
In this second case, it never comes into my WS code.
I've also tried with Job1.PostFile without result ....

Well, my question is:

How to well code an upload file in both codes with such file ?

Thanks in advance for your feedback.
 

DonManfred

Expert
Licensed User
Longtime User
I suggest to use Job.PostMultipart (See my Signature for an Example)
I can´t help on VB
 
Upvote 0

Yayou49

Active Member
Licensed User
I suggest to use Job.PostMultipart (See my Signature for an Example)
I can´t help on VB

I've tried to add your solution to my project but I get this error with line
B4X:
req = MultipartPost.CreatePostRequest("http://www.vdfb.de/b4a/multipartpost.php", NV, files)
Compilation du code Java. Error
B4A line: 187
req = MultipartPost.CreatePostRequest(\
javac 1.8.0_121
src\xxxxxx\V1\phototest.java:520: error: incompatible types: HttpUriRequestWrapper cannot be converted to OkHttpRequest
_req = (anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpRequest)(mostCurrent._multipartpost._createpostrequest(mostCurrent.activityBA,"http://www.vdfb.de/b4a/multipartpost.php",_nv,_files));

Library Http (1.36) and okHttp (1.20) are online....
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which format does your server expect? Maybe you need to base64 encode the raw images.
Your current code will create a huge json string. It will be very inefficient.
Log the generated json string and you will see it.

I've tried to add your solution to my project but I get this error with line
No need to add anything. OkHttpUtils2 already supports with HttpJob.PostMultipart.
 
Upvote 0

Yayou49

Active Member
Licensed User
No need to add anything. OkHttpUtils2 already supports with HttpJob.PostMultipart.

Erel,
I'm a bit lost whith Http, Okhttp, 2, ....
I have a solution working fine since long time and trying to implement new things, I'm facing some errors whith library.

Could you please tell me which library are no more used (and can be removed) between:
- Http
- HttpUtils2
- OkHttp
- OkHttpUtils2

Thx
 
Upvote 0
Top