Save text file on client machine using javascript - javascript

I want to create text file on client machine and then want to write in that file.
And while saving that file i want to ask user where he want to save file i.e. want to ask by save dialog box....
Thank in advance....

That is not possible with JavaScript for security reasons. You cannot create files on the client machine, since JavaScript doesn't have write permission in that environment.
What you could do as a work around is to send the text that you want to store through an Ajax-request to the web server, which in turn can generate a the text-file for the user to download to a desired location. Not as smooth, but at least it is a work around.

You cannot write back using JavaScript alone. You would have to perform a GET or POST call to be able to prompt a save.
Related question Read/write to file using jQuery

You can do it through flash "proxy" that uses FileReference class and communicating with flash using ExternalInterface

The data I wanted to write is already available at the client side (queried from server) and stored in Javascript arrays.
I have done this using 'Data Tables' which is a plug-in for the jQuery Javascript library. You can store the data in .csv, excel or also .pdf. You can access this nice plug-in at http://datatables.net/

Related

Creating and editing .text file from javascript

I am making a web site that makes a record of the person who visit it
I have created a html form page that requests the name from the user
But I am unable to figure out how to keep record of it,maybe something like using making a text file and having javascript add names to it,but I am not sure how to do it
I am using Google drive and drv.tw to upload the site so I can't use any server side scripting language
Native JavaScript has not access to user's file system. But Node.js has, try this one if u can. In native JS you can use something like localStorage. But this solution only for your task, do not use this for production...

Is it possible to transfer information through js?

It may not be a good question, but is it possible to transfer local values from a user to a public text file using JS?
For example, I want to write the IP value of a user ( var IP = ... ) to a text file (www.blabla.com/iplist.txt ).
With only JavaScript? Yes. With only browser-based JavaScript and nothing else? No. :-)
You need
A place that stores the file so that when people go to look at it, they can get it (e.g., a server of some kind), and
Some means of modifying the file that that server provides when people go to it, which you can't do from browser-hosted JavaScript
You could use a Node.js-based server (for instance), or some cloud provider where you don't care what the server tech is, but you'll need a server.
Thank you, guys! I will use PHP for that.

How do I handle local file uploads in electron?

I'm having a hard time figuring out how to approach file uploads in atom electron. I would post code but I don't even know where to begin with this one.
In a standard web app I would post from the client to the server, either via a standard postback or using an ajax request. I have some pretty cool solutions for that. But in the case of electron, I'm not sure where or how to "post" the file back. I guess I just want to access the contents of my <input type='file' /> from node.js. How do I do this?
I could post to the browser process, but I don't know what the "address" would be. Or do I need to create a separate "page" in my app just to accept form posts? My background in web-dev is probably blinding me to some obvious answer, can someone help?
EDIT
Just to add a little more context, I have a .csv file which I'd like to allow the user to upload. I will then process this using node-csv and insert each returned row into the app's nedb datastore.
If you're going to process the file on the user's machine then there is no need to upload the file anywhere, it's already exactly where you need it. All you need to do is popup a dialog to allow the user to browse their file system and select the file they want your app to process. You can create a button and call dialog.showOpenDialog when the user presses it, that will get you a filename and you can then use Node's fs.readFile to read it from disk, then you can go ahead and process the contents in whichever way you want.

HTML & JS: read local database

I want to make some sort of website on a USB stick which opens in a browser. The HTML file should be able to read a (sql-)database in the same folder using javascript. It's a little application for me. These are my files:
USB stick:
-- start.html
-- database.sql
HTML5 offers "local storage" and it works perfectly. But with this method I can't access a local, already existing database on the stick. The browser creates a separate, emtpy database and saves it not on the stick. I don't need to write datasets.
Is there a different method to read databases? Thanks!
You can use sql.js, which is a SQLite converted to JavaScript. You'll have to provide your own import/export functions though and, naturally, you can't export to filesystem in JS, but since you only intend to read, that won't be a problem.
You are not going to be able to get the web browser to run a sql query. The best thing you could do is have it load data using ajax from the local file. It is not possible to save the updated data base to the local file system however.
You could have the javascript read comma separated data or the data could be stored in xml or json.
If you are wishing to save data however I believe a desktop application is needed
If it is just for reading, it is possible to just read and parse a file using javascript. But I wouldn't use an SQL file, but rather a file containing JSON instead. I don't think there are JavaScript libaries that can read SQL, and it won't be an easy job to write one.
Mind though, that even if you manage to find one, all of the file's contents will be read into memory, and that it is not possible to write the file.
Since you want a local running database application, I would very sincerely advise you not to use HTML and Javascript, but rather Java or any native language that can read the database and do some proper memory management. You may stick to HTML/JS for the GUI, if you want to, but you may find it just as easy to leave HTML out of it altogether.
What you want now, is very uncommon. Database applications tend to have a backend, and websites tend not to be run from a USB stick.
The problem is that local storage is local to the system the browser is running on because it's local to the browser not from where the files originated.
This means you'll have to deliver the data to the browser for it to store the data locally. Right now your data is located on your stick.
You have a couple of options:
1. Deliver the data to the browser to create your local storage database via javascript.
2. Use Rob W's answer and supply a portable browser (good idea Rob!)
I've made no assumptions how the data is stored in your .sql file. If it's actually SQL, you're out of luck. Localstorage is not a sql database.
Good Article on the basics of local storage in HTML5
http://diveintohtml5.info/storage.html

Reading and writing to an external file using javascript?

I have an online storage account that I`m using for my homepage. Basically I have just made an "index.html" and stored there . and no php , asp is possible .
So If I must create a message form on the homepage and store the message in a separate text file in JSON format ,can it be done using javascript ?
also I need to query the Text file whenver I want to display the messages using javascript .
So far , I tried TaffyDB but realised it doesn`t have a way to persist the data after session closes. or maybe I missed something?
Thanks!
Short answer. No.
The JavaScript is client side. So it can do all sorts of cool stuff on the persons computer that visits your site but unless you're running some server side code that takes the JSON encoded data and does something with it then you're out of luck.
There are many alternatives.
If you don't want to run your own server side code then you could use a separate service like Parse.com that does REST and has a comprehensive API.
A mobile website can access Parse data from Javascript.
A webserver can show data from Parse on a website.
You can upload large amounts of data that will later be consumed in a mobile app.
You can download recent data to run your own custom analytics.
Applications written in any programming language can interact with data on Parse.
You can export all of your data if you no longer want to use Parse.
You can try with jQuery/AJAX. To read:
$.get("path_to_file", null, function(fileData) {
alert(fileData);
/* Your code goes here */
}, "text");
But in order to write, I think the only way is with some server-side language (PHP, ASP, etc)
The short answer is no.
You need to have some server-side support to persist the data on that server. You can, however, use client-side javascript to relay the information to a server that DOES support reading and writing of the data of course.
Technically, node.js is javascript that does support file reading and writing - but I assume that's out of the question for your environment :)
One crazy way (just as a thought experiment) to implement persistent storage for your web application without server side support is to have the clients talk to each other through P2P. This is possible with Flash or some java applet..etc. So as long as one client is up (perhaps your own comupter!), you'll have some form of persistent storage. Your server/webpage simply serves up this embedded object which does the actual work.

Categories

Resources