Save and read file with JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Save and read file with JS

var myxml = String.raw`<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc"> <Text text="Hello Here is Oma FAlk"/> </mvc:View>`; //alert(myxml) f = fopen("/.App.view.xml", 3)// opens the file for writing alert(f) fwrite(f, myxml);// c Is this the correct way to read/write files in JS? try to create a xml file in same directory as html and js file. https://code.sololearn.com/Wa18A231A13A/?ref=app

28th Feb 2021, 11:34 AM
Oma Falk
Oma Falk - avatar
7 Answers
+ 5
Use fetch method to load a file. To write a file to download folder, you need to create download link with click action. https://code.sololearn.com/WMRV3ZREEniR/?ref=app https://code.sololearn.com/W14L3XZ9JqGR/?ref=app
28th Feb 2021, 11:16 PM
Calviղ
Calviղ - avatar
+ 3
It is possible via an indirect way, using a Blob Object and a download link (see example). This method does possibly not work inside the Sololearn code playground in the mobile app (at least not on my iPhone), but it works in every webbrowser or the code playground of the Sololearn Website. https://code.sololearn.com/WA14a21a6a0A/?ref=app
28th Feb 2021, 4:03 PM
Michael
Michael - avatar
+ 2
i am writing data to json in localstorage. Localstorage works for ios but not in android app in case json is an option for you
28th Feb 2021, 1:37 PM
bell
bell - avatar
+ 2
Michael would your solution work from an app? were would it write to?
28th Feb 2021, 7:08 PM
bell
bell - avatar
+ 2
@bell depends on the parser and/or the restrictions of the OS. If the download attribute of the <a> tag is supported and File API is accessable, it should be possible.
28th Feb 2021, 7:21 PM
Michael
Michael - avatar
0
Martin Taylor no..... these are my tries. Ok I can't... Must do the UI dynamically.
28th Feb 2021, 1:22 PM
Oma Falk
Oma Falk - avatar
0
ok
12th Mar 2021, 2:22 PM
Dede Haidar
Dede Haidar - avatar