How do I write data to a file with Javascript? - javascript

I made a game with javascript using this tutorial as a base: http://html5gamedev.samlancashire.com/making-a-simple-html5-canvas-game-part-3-drawing-images/
How do I get it to write the data from the item counter (var itemCounter = 50;) to a text file named savedata.txt? I googled it, but no helpful results came up. Can someone help me?

Technically, you can create a server with nodejs [which is built with javascript]. Details can be found here

Its not possible to store the data as a file on the client.
But you can use localstorage, websql, indexeddb or simply cookies for it.
Note that all of these storages have different properties in terms of lifetime.
You could also create a blob using the blobapi and then create a dataurl and request the user to save it, using drag and drop + fileapi to read the data, this approach however will make it easy for users to modify the data.

Writing a file is posible with the new FileWriter and FileSystem APIs.
More mature solutions (not using files) have already been mentioned

Javascript does not support working with files, for data storage several options are available:
cookies
Local Storage
Server side storage

Related

Full CRUD app without a database possible?

If I use a json file stored in one of my GitHub repos as a mock backend, I know how to fetch and read all the data. Is it also possible to edit or post new data to this json file? Would an alternative mock backend like Mocky.io be a better solution (to achieve full CRUD)?
I think you could store the information inside csv files or somehting like that, you would be recreating a database engine as MongoDB & create your own reader to find the info, or you could store the users info using local Storage,
However this would make your app very limited.
Here's the link for the documentation of local storage
https://developer.mozilla.org/es/docs/Web/API/Window/localStorage
Well if you want to try out CRUD operations you can use free JSON APIs like http://jsonplaceholder.typicode.com/ or
https://mockfirst.com/
where you can create, read, update and delete data using various api end points. It is better to go this way first then you could move on to updating a JSON file.
(UPDATE)
You can use https://jsonbin.io/
Here you can place your own data and use it as an API.

Write json to local disk javascript

I wrote a small application(proof of concept) in angular.js and i encountered the need of persisting simple data between application instantiations.
I would like to know if there's a way of storing JSON objects to a local file, on my computer, without server or database intervention.
The easiest solution I found is using ColdFusion, but this requires setting up a server too.
Thanks!
You can use HTML5 Local Storage.
you can store Json object like this:
var jsonObejct = {'city':'New Yourk', 'country':'USA'}
localStorage.setItem('jsonObejct',JSON.stringify(jsonObejct));
you can retrieve from Storage Like:
var loadedObj = localStorage.getItem('jsonObejct');

Save JSON changes locally

So I have a little app that I built that allows me to input my class details and it parses it into JSON. The basic structure is:
var $classes = {
className : {
subject: $subject;
teacher: $teacher;
homework: [];
}
}
I have an interface that will create a new object within the $classes object itself, effectively adding another class to my class list. I don't really want to host this program on a server, so the problem I'm having is getting the browser to save the changes made to the action JS file. As it stands, any change I make is wiped on a refresh.
Is it possible to manipulate a local JS file via a browser?
Can I setup a localhost on my machine and somehow us JS to do what I need?
I suspect I'll need a server-side language to do this, but I want to be 100% sure. If so, do you think Node.JS would serve me well? I'm trying to continue my JS track before jumping into another language if possible.
Thanks!
I'm not sure I completely understand your needs, but have you looked at the local storage API? It was added in HTML 5 and allows saving key value data locally in the browser. This would allow you to run your app in the browser without a server backend.
See http://diveintohtml5.info/storage.html for more info on the local storage api. Hope that helps.

Javascript Decryption during Download

I'm building an ASPX website that should allow the user to download a CSV/Excel file (including the 'Save To' dialog). The CSV contains encrypted data - the decryption key is available at user side and should be kept secret against the webservice.
So decryption actually should be performed within the browser, a javascript implementation (sjcl) has proofed to work fine.
But how can the incoming datastream during a file download be influenced? Something like a browser hosted proxy performing the javascript decryption?
#closure: thanks a lot! Ajax is no problem, and the idea
<a href='data:application/csv;base64,aGVsbG87d29ybGQNCg=='>click</a>
is really cool, but it has two problems: it seems not work with IE and it is not the right approach for really huge tables. The solution should be able to handle many thousands of records, therefore we need some sort of download stream encoder/decrypter.
Here are the steps to achieve this:
Instead of downloading the CSV directly to the client machine, fetch it via ajax
Once the data is received in via Ajax, parse the CSV via many available functions on internet. Let me know, if you need help on this. This function will convert the CSV to native Javascript Arrays.
Walk through the Array and covert the encrypted data to unencrypted data. Do it natively in the same Array.
Convert the array to CSV (Again there are functions in public domain)
Make a link (a element) and set the href to local data like data:text/csv;charset=utf-8, + encodeURIComponent(csv)
Present this link to the user and ask him to click on it to save the file locally.

How to access a property file using "javascript"

How do I access a property file using javascript. Normally property file is a xml based file.
I java we access a property file like this:
Properties prop = new Properties();
fis = getClass().getResourceAsStream("props.xml");
if (fis != null) {
prop.loadFromXML(fis);
}
String dbUrl = prop.getProperty("dburl");
I want to do the same but using javascript. is there a possible way of doing it?.
JavaScript can't load files, as part of its security model. It can retrieve XML from the server using AJAX, but it can't read files from the client computer.
You can't load any files from the users computer with javascript in the browser.
If the file is from your own server you can load it, like any other ajax, with XMLHttpRequest.
Javascript doesn't use property files, as, either it has all the information it needs in the javascript files or in the html, or it will make an XMLHTTPRequest call to get the information from the server.
The server can look at the property file, and may use information passed in from the request, such as the header information, to know more about the client, to determine what information to pass back.
So, if you want to pass back some localized information, the server would have to get that from the browser request and then it could send back just what is needed for that transaction.
Javascript is different from java, so one limit is that javascript cannot read from the hard drive of the user, and since it is a webpage, the user wouldn't have the property file installed, it would still be on the server.
Javascript can only make requests to the address that that script came from, so there is a second sandbox rule that has to be met.
You may want to better understand javascript, then try to rephrase your question.
HTML5 now allows JavaScript to read local files, via the File API:
http://www.html5rocks.com/en/tutorials/file/dndfiles/

Categories

Resources