B4J Question BANano: Reading Local Files (JSON, Text) from User's Disk

luki_c

Member
What is the simplest way in BANano to read a local file's content after a user selects it with <input type="file">?
 

Mashiane

Expert
Licensed User
Longtime User
1. For your input component, you need to add to it a "change" event.
2. Then when that change event happens, you can get the contents of the file.

See this example, it has source code on the BANanoFiles


PS: After the change event is fired and you process the file, you need to NULLYFY the value of the input otherwise you wont be able to select the same file again.

All the best.
 
Upvote 0

luki_c

Member
Thank you for your help, I am still having some problems with this module:


B4X:
BANano.GetElement(".btn-file").On("click",Me, "File_rw")


Sub File_rw(e As BANanoEvent) ' buttons
    Dim el As BANanoElement = BANano.ToElement(e.Target)
    Dim parentList As List = el.Closest(".btn-file")
    Dim pr As BANanoElement = parentList.Get(0)

    If pr.GetAttr("id")     = "config-save" Then
        Log("SAVE")
       
        Dim colectData As BANanoElement = BANano.GetElement(".mod-param")
        Dim field As BANanoElement
        Dim index As Long
        Dim jsonMap As Map
        jsonMap.Initialize
        colectData.EachStart(field,index)
'        Log(field.GetAttr("id") &":  " & field.GetValue)
        jsonMap.Put(field.GetAttr("id"),field.GetValue)
        colectData.EachEnd

    Dim json As JSONGenerator
    json.Initialize(jsonMap)
        SaveAs("moj_plik.json", "text/plain", json.ToPrettyString(1))
    Else If  pr.GetAttr("id")     = "config-load" Then
        Log("LOAD")
        bf.ShowFileSelect("fuconnect")
    '    BANano.GetElement("#fileLoadCfg").RunMethod("change", Null)
    End If
End Sub

error:
Uncaught TypeError: Cannot read properties of undefined (reading 'click')

I am building the layout in Bootstrap Studio and I am not using the Skeleton libraries, if that makes any difference.
 
Last edited:
Upvote 0

luki_c

Member
Thank you very much for your assistance. It turns out the problem was related to the incorrect parent container ID in AddFileSelect. The container I was calling was not directly the body, but another element. Using the correct ID, main-content, has successfully fixed the issue
B4X:
bf.Initialize.AddFileSelect(Me, "main-content", "fuconnect")
 
Upvote 0
Top