B4J Library [ABMaterial]: Attempting a file chooser

Ola

Goal: Ability to upload files to the server, after the files are uploaded, clear the list of files to start all over again.

MashAttachments.gif


The original source code is available here.

In Class_Globals

B4X:
Dim ma As MashAttachments
    Dim uploads As Int = 0   'IMPORTANT

uploads is necessary to keep the number of files uploaded. As soon as the number of files uploaded matches, then you can clear the control. Clearing the component is necessary because doing another upload does not clear the file list after upload. This is a known issue. When the file list is cleared, the default title is also reset (see gif above)


In ConnectPage

B4X:
ma.Initialize(page,"ma","Select file(s)...",True)
ma.ZDepth = ABM.ZDEPTH_1
    page.Cell(1,1).AddComponent(ma.ABMComp)

No external dependencies needed. Explaing the initialize method...

  • "Select files(s)..." title to appear, you can have this blank if you dont want it to show.
  • True - boolean to turn multi-file selection on or off.
Events

We call the .Upload method just after the change event of the control is fired.

B4X:
'upload a file as soon as its selected
Sub ma_changed(value As Map)
    'reset the number of files to upload to the server
    uploads = 0  'IMPORTANT
    'upload files to the server
    ma.upload
End Sub

'we can detect the progress of the file uploaded
Sub ma_progress(value As Map)
    Dim files As String = value.Get("value")
    Log(files)
End Sub

'when the file upload is complete
Sub ma_complete(value As Map)
    Dim files As String = value.Get("value")
    Log(files)
End Sub

other events

_progress, _complete, _error, _abort

For Page_FileUploaded, we increment uploads when a file is uploaded
when .GetCount = uploads, we clear the control to enable another upload.

B4X:
Sub Page_FileUploaded(FileName As String, success As Boolean)
    myToastId = myToastId + 1
    If success Then
        page.ShowToast("toast" & myToastId, "toastgreen", "File " & FileName & " uploaded!", 3000, False)
        uploads = uploads + 1
    Else
        page.ShowToast("toast" & myToastId, "toastred", "File " & FileName & " not uploaded!", 3000, False)
    End If
    Dim actualFile As String = File.combine(File.DirApp, DownloadFolder & FileName)
    page.ws.Flush ' IMPORTANT
    
    'if we have uploaded enough files, clear
    If mu.GetCount = uploads Then
        mu.clear
    End If
    
    'if we have uploaded enough files, clear
    If ma.GetCount = uploads Then
        ma.clear
    End If
    
End Sub


To do (perhaps)

  • Ability to change the colors

Ta!

Enjoy...
 

Attachments

  • MashAttachments.bas
    8.4 KB · Views: 381
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
My abm is 4.30 but HTMLElememt no addcssclass function.

Error description: Unknown member: addcssclass
 
Top