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>
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
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...
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
abc_ASD.txt is
/storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/abc_ASD.txt
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
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.