Android Question Show progress of upload file

Blue.Sky

Active Member
Licensed User
Longtime User
Hi
I use PostString to upload file example below code
How can i show progressing of send file
B4X:
    ht.Initialize("upload",Module)
    ht.PostString(Library.WebService & "/upload_file",su.EncodeBase64(ou.ToBytesArray))
    ext = getFileExtension(sFile)
    stype = getFileType(ext)
    ht.GetRequest.SetContentType(stype)
    ht.GetRequest.SetHeader("key1",Library.LoginSession.Username)
    ht.GetRequest.SetHeader("key2",Library.LoginSession.Password)
    ht.GetRequest.SetHeader("key3",Library.phone1.GetDeviceId)
    ht.GetRequest.SetHeader("key4",Library.LoginSession.Token)
    ht.GetRequest.SetHeader("path",sParentPath)
    ht.GetRequest.SetHeader("name",Library.GetFilename(sFile))
    ht.GetRequest.SetHeader("extension",ext)

And i php i get file with below code
PHP:
        $content = base64_decode(file_get_contents("php://input"));
        $len = strlen($content);
        $headers = apache_request_headers();
        
        foreach ($headers as $header => $value) {
            if ($header == "key1")
                $username = $value;
            if ($header == "key2")
                $password = $value;
            if ($header == "key3")
                $dev_id = $value;
            if ($header == "key4")
                $token = $value;
            if ($header == "extension")
                $ext = $value;
            if ($header == "name")
                $name = $value;
            if ($header == "path")
                $path = $value;
        }
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to use HttpUtils2 source code. Modify PostFile and use CountingInputStream from the RandomAccessFile:
B4X:
CountingStream.Initialize(in)
req.InitializePost(Link, CountingStream, Length)
CountingStream should be a global variable.

Now you can use a timer to monitor the progress by checking the value of CountintStream.Count
 
Upvote 0

Blue.Sky

Active Member
Licensed User
Longtime User
You will need to use HttpUtils2 source code. Modify PostFile and use CountingInputStream from the RandomAccessFile:
B4X:
CountingStream.Initialize(in)
req.InitializePost(Link, CountingStream, Length)
CountingStream should be a global variable.

Now you can use a timer to monitor the progress by checking the value of CountintStream.Count
Thank you but i cannot change PostString(upload) to PostFile
Cannot i shpw progress in PostString?
 
Upvote 0

Blue.Sky

Active Member
Licensed User
Longtime User
How large is the string? Unless the string is huge then the progress will not be very meaningful.
I programming app that upload any file to host.
maybe large or maybe small
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thank you but i cannot change PostString(upload) to PostFile
Cannot i shpw progress in PostString?

you can try to use this (untested!)

Activity or starter
B4X:
Sub Process_Globals
    Dim cs As CountingInputStream
End Sub

httpjob.bas
B4X:
Public Sub PostBytes(Link As String, Data() As Byte)
    Dim in As InputStream
    in.InitializeFromBytesArray(Data,0,Data.Length)
    Main.cs.Initialize(in)
    req.InitializePost(Link, Main.cs, Data.Length)
  
    'req.InitializePost2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
 
Upvote 0

Blue.Sky

Active Member
Licensed User
Longtime User
you can try to use this (untested!)

Activity or starter
B4X:
Sub Process_Globals
    Dim cs As CountingInputStream
End Sub

httpjob.bas
B4X:
Public Sub PostBytes(Link As String, Data() As Byte)
    Dim in As InputStream
    in.InitializeFromBytesArray(Data,0,Data.Length)
    Main.cs.Initialize(in)
    req.InitializePost(Link, Main.cs, Data.Length)
 
    'req.InitializePost2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
It test it and correctly worked.Thank you
 
Upvote 0
Top