Save and read file with JS | Sololearn: Learn to code for FREE!

+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

2/28/2021 11:34:01 AM

Oma Falk

8 Answers

New Answer

+5

You can't read or write to the file system using JavaScript in a web browser. Lines 5 to 19 are commented out in the code so it isn't being executed anyway. The commented out code looks like it's for a mobile application judging by the App.view stuff so it isn't useable in a web page. Did you copy this code from somewhere?

+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

+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

+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

+2

Michael would your solution work from an app? were would it write to?

+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.

0

Martin Taylor no..... these are my tries. Ok I can't... Must do the UI dynamically.

0