Save files in HTML5

Javi400

New member
Joined
Jan 21, 2023
Messages
14
Hi!
I am making a map editor in HTML5 target. How can I do so that the user can save the map on his computer? Can I open a savedialog type window?
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,504
Nothing build into CX. I have to study how other web base editors do it.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
It's super easy in Cerberus-X. Look here.

Mind you that i have not checked Safari yet it might need uncheck safety checks or you might go around it using a modification.

This works on Chrome and Firefox amongst others though.

Example
Code:
' Save a string on the HTML5 target

Import mojo2

Import "save.js"
Extern
Function saveString(content:String)
Public

Function Main()
    New MyApp
End

Global canvas:Canvas

Class MyApp Extends App

    Method OnCreate()
        canvas=New Canvas
        SetUpdateRate 0
        saveString "It works!"
    End Method
   
    Method OnUpdate()
        If MouseHit(0) Then saveString "It works!"
    End Method
   
    Method OnRender()
        canvas.Clear
        canvas.Flush
    End
   
End Class
save.js
Code:
function saveString(content){
    uriContent = "data:application/octet-stream," + encodeURIComponent(content);
    window.open(uriContent,"Download File");
}

function loadFile(file, buf){
    var reader = new FileReader();
    reader.onloadend = function (){
        var rawData = reader.result;
        if (rawData == null){return;}
        buf._Init(rawData);
    }
    reader.readAsArrayBuffer(file);
    return buf
}
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
If you want to add that file requester you need to know that is a feature provided by the browser and not the JavaScript language itself so this is as far as you can get using state of the art web coding.

You can upload using a requester often that is supplied by the browser but not download. The user might be confused and acciendelty overwrite important files, so this is why they choose to do that. Everything goes to a "download folder", set by the browser, and any filenames that are the same gets chane so they are neveer overwritten. This functinality is inbuilt in all platforms.

EDIT If you are super into getting a file requester there is hope on Chrome they are developing a new API for that. But it is still in development so I would not use it yet. It's interesting progress.
 
Last edited:

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
Update that works also with Safari browser:

Code:
function saveString(content) {
    const blob = new Blob([content], { type: 'application/octet-stream' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'filename.txt');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  }
 

Javi400

New member
Joined
Jan 21, 2023
Messages
14
Hey!, thank you very much for your help, I understood it very well with the example, I'll try it :)
 

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,504
I guess you are covered then
 
Top Bottom