- None on the browser without JS
- None on mobile without using is
- Using the Filesystem extension on desktop
For JavaScript solutions:
-
Desktop through JS:
Import the node fs module like you normally would and use it as always (remember the relative paths are different in the preview) -
Mobile through JS:
Search on Google for filesystem Cordova. Open the link of the Cordova website with the documentation. See how to use it. Use it like described (WON’T WORK IN PREVIEWS).
Example Js usage:
Writing:
var type = window.TEMPORARY;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
alert('Write completed.');
};
fileWriter.onerror = function(e) {
alert('Write failed: ' + e.toString());
};
var blob = new Blob([runtimeScene.getVariables().get("hello").getAsString()], {type: 'text/plain'});
fileWriter.write(blob);
}, errorCallback);
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
}
Reading:
var type = window.PERSISTENT;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
fs.root.getFile('log.txt', {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
runtimeScene.getVariables()
.get("hello")
.setString(this.result)
};
reader.readAsText(file);
}, errorCallback);
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
}
Deleting:
var type = window.PERSISTENT;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
fs.root.getFile('log.txt', {create: false}, function(fileEntry) {
fileEntry.remove(function() {
alert('File removed.');
}, errorCallback);
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
}
Export as manual build Cordova. Open the shell and go to the directory you exported to. Make sure you have Cordova installed. If you haven’t, you need to download NodeJS (search on Google) and type into the shell:
npm install -g Cordova
Then type:
cordova plugin add cordova-plugin-file
Now you can build it with phonegap build or the tutorial manually building with Cordova on the wiki.
- For the browser through JS:
Use the HTML5 FileAPI.
Example:
Reading
// Trigger Once this bloc
let e = document.createElement("input")
e.setAttribute("type","file")
e.setAttribute("style","display:none")
e.addEventListener("change", function(){
var reader = new FileReader();
reader.onload = function(loadedEvent) {
if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading = {};
gdjs.evtTools.filereading.data = loadedEvent.target.result;
gdjs.evtTools.filereading.updated = true
}
reader.readAsText(this.files[0]);
if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading {};
gdjs.evtTools.filereading.fs = e;
// Run each Frame this block
if (gdjs.evtTools.filereading === undefined) gdjs.evtTools.filereading {};
if(gdjs.evtTools.filereading.updated === undefined) gdjs.evtTools.filereading.updated = false;
if (gdjs.evtTools.filereading.updated) {
runtimeScene.getVariables()
.get("variable name")
.setString(gdjs.evtTools.filereading.data)
}
// To actually load the file to the predefined variable
gdjs.evtTools.filereading.fs.click()
This solution requires the user to select a file from the explorer and is not the most user friendly option because of that.