B4J Question [BANano] BANano.GetFileAsArrayBuffer returning something whilst file does not exist.

Mashiane

Expert
Licensed User
Longtime User
Hi

I am reading a file from the assets folder, with an existing file all is good. Thing is, I specified an non-existing file, so that I can trap if an error happens, yet the "res" returns content and thus an error is never returned. Is there a way around this?

B4X:
Try
        Dim res As Object
        Dim err As Object
        Dim ab As BANanoPromise = BANano.GetFileAsArrayBuffer(assetFile, Null)
        ab.ThenWait(res)
            Log("buffer")
            Log(res)
        ab.ElseWait(err)
            Log("error...")
            Log(err)
        ab.End

File Does not exist response.

1697625479453.png
 
Solution
That method assumes the file exists. But it's not that hard to write one that does catch it if the file does not exist.
Big advantage over first checking if the file exists is that you do not have to do an extra request to the server.

B4X:
Sub GetFileAsArrayBuffer(assetFile As String) As Object
    Dim error As Object
    Dim fetch As BANanoFetch
    Dim fetchResponse As BANanoFetchResponse
    Dim blob As BANanoObject
    Dim buffer As BANanoObject
    Dim prom As BANanoPromise
    Dim bytes() As Byte
    
    prom.NewStart
        fetch.Initialize(assetFile, Null)
        fetch.Then(fetchResponse)
            If fetchResponse.OK Then
                ' return the blob to the next then
                fetch.Return(fetchResponse.Blob)...

alwaysbusy

Expert
Licensed User
Longtime User
That method assumes the file exists. But it's not that hard to write one that does catch it if the file does not exist.
Big advantage over first checking if the file exists is that you do not have to do an extra request to the server.

B4X:
Sub GetFileAsArrayBuffer(assetFile As String) As Object
    Dim error As Object
    Dim fetch As BANanoFetch
    Dim fetchResponse As BANanoFetchResponse
    Dim blob As BANanoObject
    Dim buffer As BANanoObject
    Dim prom As BANanoPromise
    Dim bytes() As Byte
    
    prom.NewStart
        fetch.Initialize(assetFile, Null)
        fetch.Then(fetchResponse)
            If fetchResponse.OK Then
                ' return the blob to the next then
                fetch.Return(fetchResponse.Blob)
            Else
                ' throw an error so it goes to the 'Else' part of the fetch
                BANano.Throw("File not found")
            End If
        fetch.Then(blob)
            ' return the arrayBuffer to the next then
            fetch.Return(blob.RunMethod("arrayBuffer", Null))
        fetch.Then(buffer)                            
            ' return the arrayBuffer    
            BANano.ReturnThen(buffer)
        fetch.Else(error)
            ' log the error (can be our thrown 'File not found')
            Log(error)            
            ' return False
            BANano.ReturnThen(False)
        fetch.End
    prom.NewEnd
    
    Return prom    
End Sub

Usage (extra how to convert it to a normal B4J byte array if successful):
B4X:
Dim prom As BANanoPromise = BANano.Await(GetFileAsArrayBuffer("http://127.0.0.1:8089/assets/banano1.jpg"))
If prom = False Then 'ignore
        Log("Woopsie")
Else
        ' create a Uint8Array 'view' (an array of bytes) to access the ArrayBuffer
        Dim Uint8View As BANanoObject
        ' can be Uint8Array, Uint16Array, Uint32Array or Float64Array
        Uint8View.Initialize2("Uint8Array", prom)
        ' convert it to a B4J byte array so we can access it more easily
        Dim bytes() As Byte = Uint8View
        For i = 0 To bytes.Length - 1
            Log(bytes(i))
        Next
End If

Alwaysbusy
 
Upvote 1
Solution
Top