JavaScript, what is the correct address in fetch()?

We have a text file abc.txt, through fetch(my_file) we introduce its content in the script and obtain its ASCII codes.

abc.txt and my_script.htm in asset.

abc.txt

one
two
three
abcdefghijklmnopqrstuvwxyz
1
2
3

my_script.htm

Running script.
<script>
my_file = AppInventor.getWebViewString(); // INPUT
// import my_file from '/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt';
//           my_file = 'file://storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt';

fetch(my_file).then(function (response) {
    // get the data as a Blob
 if (!response.ok) throw alert("fetch failed");
    return response.arrayBuffer();
})
.then(function (ab) {
    var data = new Uint8Array(ab);
   AppInventor.setWebViewString("" + data); // OUTPUT
});
</script>

fetch22

Works!

fetch11

1 Like

Now we copy my_script.htm and abc.txt as abc_ASD.txt in ASD.
What address should we put in fetch(my_file) for it to recognize abc_ASD.txt?

fetch_JS.aia (3.5 KB)

I have tried with absolute address, but it doesn't work for me.

Neither with relative direction

Did you try to use only the filename to fetch from the same directory?

EDIT: from your Stackoverflow answer:

fetch will load the path relative to the url you have opened

Taifun

I did try my_script.htm and abc_ASD.txt in the same directory.
I also tried
/../../abc_ASD.txt
/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt
./storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt
file://storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt

htt p://storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt

Theoretically it should work like this...

Did you check, if both files are in ASD?
In your copy blocks you are using additionally a /... this might be wrong...

EDIT: what exactly is localhost? The assets or ASD? In both examples you are calling the same script... In the second example you should call the script in the ASD...

Taifun

Files in ASD (Android 9) (MIT Companion)
fetch55

I have tried
http://localhost/my_script.htm
and
http://ASD/my_script.htm

my_script.htm file in asset is (Android 9)(MIT Companion)
/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/AppInventor/assets/my_script.htm

fetch66

abc_ASD.txt is
/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt

that's why i tried

/../../abc_ASD.txt

This does not exist
For the script try the full path to ASD, for the file only the filename

Taifun

Excuse me, with ASD I wanted to simplify

http://storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/my_script.htm

this url does not exist, it should start with file:/// like this
file:///storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/my_script.htm

Taifun

It also doesn't work with all three bars. file:///

The file my_script.htm starts, because I get the initial text "Running script", but the fetch() does not find the file abc_ASD.txt

Running script.
<script>
my_file = AppInventor.getWebViewString(); // INPUT
fetch(my_file).then(function (response) {

I tried this but without luck for the ASD part
fetch_JS_1.aia (3.3 KB)

I also tried Sunny's custom webviewer without success...

Taifun

Writing the address directly in the script doesn't work either.

Running script.
<script>
// my_file = AppInventor.getWebViewString(); // INPUT
// import my_file from '/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt';


my_file = 'file://storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt';

fetch(my_file).then(function (response) {
    // get the data as a Blob
 if (!response.ok) throw alert("fetch failed");
    return response.arrayBuffer();
})
.then(function (ab) {
    var data = new Uint8Array(ab);
   AppInventor.setWebViewString("" + data); // OUTPUT
});
</script>

I don't believe that fetch allows access to file URIs, certainly not if you're loading the source page/script via an http type URL since that'd be a massive security hole. You might be able to get away with it if you are going file URI to file URI, but even then I'm not certain that works and it might be browser dependent. If your file is in the assets, a relative URL will work when using the http://localhost/ technique App Inventor supports, but if you're planning to modify the file then that's not an option since you can't write to assets. What is the use case for what you're trying to accomplish?

The idea was simply to send the content of a file to the script via its URL address.
We know that we can send a plain text, or even another type of file directly to the script using the Web.UriEncode block, we can also send a file converted to String base64,... but the challenge was to send the information through "fetch" when it was not located at the root of localhost.
In this topic we manage to send by fecht: file_example_XLSX_100_ASD.xlsx when it's on localhost, but we don't get it when it's on ASD.
@Kevinkun 's code sends the content, but using base64.