The GetFileAsDataURL and other GetFile methods return a BANanoPromise and it has a BANanoFetchOptions variable passed to it. Am I right to assume that its possible for it to have a return type of BANanoFetch?
This will enable one to execute something like
B4X:
Dim bf As BANanoFetch = BANanoGetFileAsDataURL1(sPath, null)
bf.Return(fr.Blob)
You are not using the correct methods. e.g. GetFileAsDataURL retrieves the file and makes a DataURL from the result, GetFileAsText will convert the result to plain text etc... They are shortcut methods that do all the heavy lifting IF you know what you are about to receive.
For example, if you know that "tryupload.txt" is of the type text/plain you could do simply this (it will work in this case as the content size = file size):
B4X:
Dim blob As BANanoObject = BANano.Await(BANano.GetFileAsText("./assets/tryupload.txt", Null, "UTF-8"))
Dim f As BANanoObject
f.Initialize2("File",Array(Array(blob), "upload.txt", CreateMap("type": "text/plain")))
Log(f.GetField("name"))
Log(f.GetField("size"))...
I am testing with this code... and are able to see the blog on the console
Source:
B4X:
const blob = await (await fetch(dataURL)).blob();
How do I make it work?
B4X:
'convert dataURL to blob
Sub dataURLToBlobWait(sPath As String) As BANanoObject
Dim bf As BANanoFetch
Dim fr As BANanoFetchResponse
Dim blob As BANanoObject
'
Dim dataUrl As String
Dim dataUrlProm As BANanoPromise = BANano.GetFileAsDataURL(sPath, Null)
dataUrlProm.Then(dataUrl)
bf.Initialize(dataUrl, Null)
bf.Then(fr)
bf.Return(fr.Blob)
bf.Then(blob)
Log(blob)
Return blob
bf.End
dataUrlProm.End
End Sub
Usage..
B4X:
Dim af As BANanoObject = BANano.Await(dataURLToBlobWait("./assets/tryupload.txt"))
Log("outside...")
Log(af)
I am missing something as I'm able to log it internally inside the Fetch, but its "outside" fires before the rest of the code finishes?
What I'd like to do eventually is pass the Blob to the File Object with (new File > as in BANanoObject.Initialize2()), besides giving me the file info like date etc, i can just pass it to FormData.
You are not using the correct methods. e.g. GetFileAsDataURL retrieves the file and makes a DataURL from the result, GetFileAsText will convert the result to plain text etc... They are shortcut methods that do all the heavy lifting IF you know what you are about to receive.
For example, if you know that "tryupload.txt" is of the type text/plain you could do simply this (it will work in this case as the content size = file size):
B4X:
Dim blob As BANanoObject = BANano.Await(BANano.GetFileAsText("./assets/tryupload.txt", Null, "UTF-8"))
Dim f As BANanoObject
f.Initialize2("File",Array(Array(blob), "upload.txt", CreateMap("type": "text/plain")))
Log(f.GetField("name"))
Log(f.GetField("size"))
Log(f.GetField("type"))
Log(f.GetField("lastModifiedDate"))
Log(f.GetField("lastModified"))
Log("Done")
It is however better to use something more generic like this, so you can load all kind of files:
B4X:
private Sub FetchAsFile(path As String, name As String) As BANanoObject
Dim fetch As BANanoFetch
Dim fetchResponse As BANanoFetchResponse
Dim blob As BANanoObject
Dim prom As BANanoPromise
' we are going to use a Promise Wrapper as we want to use Await() for it.
prom.NewStart
fetch.Initialize(path & "/" & name, Null)
fetch.Then(fetchResponse)
' resolve the blob
Return fetchResponse.Blob
fetch.Then(blob) 'ignore
' Use ReturnThen/ReturnElse for the final result in case of a Promise.NewStart/NewEnd wrapper
BANano.ReturnThen(blob)
fetch.End
prom.NewEnd
' wait from the Promise
Dim result As BANanoObject = BANano.Await(prom)
' make a new File object
Dim f As BANanoObject
f.Initialize2("File",Array(Array(result), name, CreateMap("type": result.getfield("type"))))
Return f
End Sub
Usage:
B4X:
' here the type is a png file
Dim f As BANanoObject = BANano.Await(FetchAsFile("./assets","large.png"))
Log(f.GetField("name"))
Log(f.GetField("size"))
Log(f.GetField("type"))
Log(f.GetField("lastModifiedDate"))
Log(f.GetField("lastModified"))
Log("Done")