Is it possible use HttpUtils2 to upload file?

rossati

Active Member
Licensed User
Longtime User
hello

I have removed httputils2 and used a script of Don (thanks Don) which I include, below how I coded this:

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
   Dim dir As String = File.DirRootExternal
   camEx.SavePictureToFile(Data, dir, fileName)
   camEx.StartPreview 'restart preview   
   'send a broadcast intent to the media scanner to force it to scan the saved file.
   Dim Phone As Phone
   Dim i As Intent
   i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & File.Combine(dir,fileName))
   Phone.SendBroadcastIntent(i)
   save_and_rotate(dir, fileName)
  Dim NV As Map
  NV.Initialize
  NV.Put("width", parameters.Get("width"))
  NV.Put("height", parameters.Get("height"))
  NV.Put("action", "upload")
  Dim files As List
  files.Initialize
  Dim fd As FileData
  fd.Initialize
  fd.dir = File.DirRootExternal
  fd.fileName = fileName
  fd.KeyName = "fileUpload"
  fd.ContentType = "application/octet-stream"
  files.Add(fd)
     hc.Initialize("hc")
       Dim req As HttpRequest
'       req = MultipartPost.CreatePostRequest("http://rete.condorinformatique.com/upload.php", NV, files)
       req = MultipartPost.CreatePostRequest("http://ises.senioresitalia.org/upload.php", NV, files)
       hc.Execute(req, 1)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    If Response <> Null Then
        Log("error: " & Response.GetString("utf8") & " " & StatusCode)
    End If
    Log("error: " & Reason & " " & StatusCode)
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    out.InitializeToBytesArray(0) ' I expect less than 2000 bytes here
      Response.GetAsynchronously("Response", out, True, TaskId)
    Log("response: " & Response.GetString("utf8") )
End Sub
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
    Dim another_buffer () As Byte
    another_buffer = out.ToBytesArray
    Log (BytesToString(another_buffer, 0, another_buffer.Length, "UTF8"))
End Sub
It my be a Server problem, the commented CreatePostRequest doesn't works; you can test yourself, the site simple echoes POST variables, something like this:
B4X:
response: 1945624452<pre>Array
(
    [fileUpload] => Array
        (
            [name] => image.jpg
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpfsBzQJ
            [error] => 0
            [size] => 151663
        )

)
Array
(
    [width] => 640
    [height] => 480
    [action] => upload
)
Regards
John
 

Attachments

  • MultipartPost.bas
    2 KB · Views: 251
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
Do you mean "GET" or "POST"? My server expects POST.
If you mean the file format, my server can get any file format. If I send the files from Windows PC web client app, the server gets the files.
I tried to send files using MultipartPost example and my server also see and gets the files. But if I try Job.Postfile the server see the request, but don't see the file.
 
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
Yes. My server expects POST. As I can see that there is the difference between sending files by mutipartpost and job.Postfile. Maybe I need to change the asp.net page settings in order to get files by Job.Postfile?
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
I have upload photos from one of my apps using httputils2; the api script on the server expects multipart/form data, consisting of two text fields, USERKEY and APPKEY, which together identify the device, then the JPEG image, which the script finds in the usual $_FILES array on the server (I'm using PHP).

Happy to share the full code if you want, but essentially what I do is use stringbuilder to create my form data, appending first the boundary strings and the text fields.

I convert that to a bytes array, and initialise a stream as a bytes array, to which I copy first the headers, and then the JPEG image from the device, and finally the trailing bit of the form - the final boundary. Flush the stream, and the you can post it using the .PostBytes method, and setting the headers of the request, like this:

B4X:
Dim uploader As HttpJob
uploader.Initialize("upload",Me)
uploader.PostBytes(uploadURL,formStream.ToBytesArray)
uploader.GetRequest.SetContentType("multipart/form-data; boundary=" & boundary)
uploader.GetRequest.SetHeader("User-Agent",BLUF.blufVersion)
 
Upvote 0
Top