Android Question Upload text file with OkHttpUtils2

Doug Thorsvik

Member
Licensed User
After successfully figuring out (with assistance here) how to download an .mp3 file from the internet, I am trying upload a .txt file to the internet.

The libraries I have installed are: Core (v 8.30), OkHttp (v 1.2), RuntimePermissions (v 1.1), and StringUtils (v 1.12)

I also have HttpJob and HttpUtils2Service installed as Modules.

I tried modifying several examples from the Forum. I don't really understand MultipartFileData and the corresponding PostMultipart. I just guessed on the fd.KeyName and fd.ContentType and the first two arguments of PostMultipart. "https://gap.thorconsulting.net/joshmusic/" is the path to where I want to upload NoteTest.txt".

When I run UploadFile, I get the following error:

ResponseError. Reason: Not Found, Response: <!DOCTYPE html>

Which is followed my what looks to be an HTML document

I'm not sure what to try next.

Here is the subroutine:

B4X:
Sub UploadFile
   Log("Start upload")
   Dim upload As HttpJob
   upload.Initialize("Upload",Me)
  
   Dim fd As MultipartFileData
   fd.Initialize
   fd.KeyName = "Text"
   fd.Dir = SDpathname
   fd.FileName = "NoteTest.txt"
   fd.ContentType = "text"
  
   upload.Username = "____"
   upload.Password = "________"
   upload.PostMultipart("https://gap.thorconsulting.net/joshmusic/UploadText.php", CreateMap("param1": "value1"), Array(fd))

   Wait For (upload) JobDone(upload As HttpJob)
   If upload.Success Then
       Log("File Uploaded")
   Else
       Log("***Error: " & upload.ErrorMessage)
       ToastMessageShow("Error: " & upload.ErrorMessage, True)
   End If
   upload.Release
End Sub
 

DonManfred

Expert
Licensed User
To upload a file to a Server there must be an Serverpart running which accept the file to upload.
You can not specify just a folder on your webspace and expect you are able to upload anything there.

A httpjob.Postmultipart will create a multipart-post-request to the server. On the serverside the fileupload must be handled like a html-fileupload.

See my signature. There is an example of using a php-script on the serverside.

Or maybe post your PHP-Code so we could help more.
 

Doug Thorsvik

Member
Licensed User
I don't have any PHP code I know of. I don't how to do an html-fileupload since I'm not writing any html.

My programming experience is Visual Basic 6/MS Access on a LAN without internet access. I am converting a VB6 music player I wrote to use mp3 files downloaded from a folder at a web location. I found reading/writing files on a tablet and it's SD card very different, but I sorted it out.

I downloaded a single file from the internet (with help from Erel) without any PHP-Code that I know of.

I have had difficulty finding examples of uploading a file that I can understand. I was hoping B4A would allow me to upload a file to the internet within the language apparently it is not possible.

I went to your page on the forum, downloaded Multipart Fileupload b4A->PHP, and opened it in B4A. I have reviewed the code and could not find any php-script (PHP-Code?), just B4A.

Is everything in your B4A code to handle the serverside? Should I just be able to modify your code to upload a file? What is "http://www.vdfb.de/b4a/multipartpost.php"?

Any suggestions?

Thanks for your time.

 

Doug Thorsvik

Member
Licensed User
I appreciate the help trying to figure this out. I did not realize that uploading and downloading files was this involved!

Erel

I will have to talk to my nephew on the server details. He set up an area I could download files from and from which you have already helped me succeed!

Don

I downloaded the php code. Not sure what to do with it yet. I will talk to my nephew; I think he will know.
 
Top