How to save a var to the local computer? - javascript

I have difficulties with saving a var from javascript to a local file, so I want to write it into an existing file. I can do it with creating and downloading a file but I need it without the browser asking if and where to store the file/var. Is there any way of doing it? Need it because I want to automatically save the var so that my main program (runs locally) can get it. I scrolled through many sites but I couldn't find anything that fit my needs so I unfortunately don't have any code.
Thanks Akira

no there is no way of doing that as it is a security risk and the user needs to confirm the location of the file. The only thing you could do is set a default filename so the user would be required to overwrite the existing file.

If the main programm is running you can have the webpage talk to it via http, websockets or webrtc to localhost.

It may be preferable to use a database instead ? Your main program will retrieve the variables in it.

Related

Using javascript with a local html file to delete itself?

I coded a local html file that kind of works like windows notepad would, using localStorage with JavaScript to auto save. To make new notes, I have just been saving the open file as another .html file and naming it something new for the note. But I have found this to be really annoying, because when I want to delete the note, I have to actually go into my Files app, find the .html file for the note, and delete it.
I know how to use event listeners and whatever, but is there a line of code I can use to delete the file or even just ask the user for confirmation to delete the file? I basically need a way for a local html file to be able to delete itself using JavaScript. Everything I have managed to find online says that you need Node.js and it has to be on a server, but I wanted to ask anyway, because I wonder if permissions are different when the file is stored locally and not on a server.
No.
You can't use browser JavaScript to manipulate the real local filesystem. (You could access a sandboxed one, but not the real filesystem.)

How to search the Local file system of Windows using Javascript?

Operating system : Windows 8.
Input-from : A text field from a html page .
Input-type: text.
Output : the file that was searched.
What I want : I want the user to enter some keywords and have JavaScript search the user's local files...
Is there any way? or A javascript Library?....
And how I can query the file system about the files using Javascript..in Windows..
EDIT::
Thanks everyone for the reply...
The way I get it is ,either I have to develop my own browser based in Java that has sufficient permissions.. or
I would have to implement a file crawler that indexes everything that sends that file to the a server , so that javascript can then access it through xmlhttp requests to the server ....
This was just a curiosity and I don't want to expose anyone's personal files on the Internet.
A fun project.. That all tinkered in my mind.
This is not possible from a web page because of the browser's security restrictions. You could access the local file system with Node.js or an Electron app, but I don't know if that would suit your use case
No. That's simply not possible and even if it is, it should not be used, as it would make one's local file system open to various threats because you are directly exposing your machine on the internet. That's sufficient enough reason for a webapp not to access one's local file system in any manner whatsoever.
P.S: If you really want it, There are a few third party libraries which use Sandboxed file systems. You can try that if you want.
I havent tried it personally, but found the below link via quick googling so i am not sure if its officially supported up until now.
https://www.html5rocks.com/en/tutorials/file/filesystem/
Use <input type="file">, which implements ability for user to select file from local filesystem.

Is it possible to access a local file from a browser on a mobile device?

Question:
Let's say I create an android game that saves a save file(in the form of a text file) on the local android internal storage and I know the absolute path where the file is saved. Is it possible to create a website with a built in script that retrieves this textfile and displays the save data to the user? If not, why and are there any unconventional ways to get around it?
From my own research, I have seen similar topics give pretty mixed results. Some say that the browser is sandboxed, while others say that it's now possible. Any clarification would be appreciated! Thanks in advance!
It may be possible if you have a local web server running on your Android Device. If you can save the file to the Web Server Document folder, you can then access the file from your browser. I haven't tried it, but it sounds logically possible.

Add checkboxes at runtime and save them in HTML file code

I'd like to create an html (with javascript) file where I'd like to add/remove checkboxes at runtime, save them in the html code so that they are available when the file is opened next time. I do not want to use use a server (so no JSP or PHP).
Please tell me whether it is possible just by using javascript? If it is, please point to resources where I can get more information about it.
It is possible using HTML5 local storage or even client database storage. If you don't know whether the browser supports them yet, another possibility is to use persistent cookies.
I'm pretty sure this isn't possible in the way you request solely in javascript in-the browser (javascript cannot write to the source file).
You might have some luck however storing the definition of what checkboxes you've added in localStorage for example, then reload/redraw that same definition on reload of the page.

Is it possible to "upload" a file only with client side ? (no server involved)

I am trying to do a simple thing:
Let the user choose a txt file, and save its context to be used on the client side only.
no server side needed.
Is it possible ?
Thanks.
It is possible to do so with HTML5 Files API as explained in these resources:
https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
http://www.html5rocks.com/en/tutorials/file/dndfiles/
I guess you mean "save its content" and conclude you want to do anything with this content on the client side, e.g. extract some parts to fill a form. Anyway saving the whole file unchanged, on the same machine where it comes from, does not make sense.
So the problem is not how to upload, but how to open/read a file. You can do this with a Java Applet, Flash, Silverlight, ActiveX ... just to name a few.
JavaScript is not an option. It cannot access the file system.
If the html page, that is hosting your javascript, is from a remote server. This script is not trusted to do actions on your local filesystem.
<Obscure solution mode level = 1>
You can give more trust to a page, but this is something your user has to do. If this is an app/web only for use within an enterprise, you can probably do this centrally. And every browser handles this differently. So it is not something you can rely on, when you do not have a limited userbase.
<Obscure solution mode level = high>
If your (enterprise) users are using Internet Explorer, you could also create a HTML Application (simply give your html page an hta extension). These pages have full trust, but can only be started from a trusted location, or require confirmation from the user.
The only way you can acheive this successfully is to build an ActiveX type plugin/component (or java applet) you will have much more control of the client machine.
No. JavaScript cannot access the local filesystem.
However, you could install a webserver on your machine and e.g. run PHP on that one. Then you could do it without ever sending your data over a network connection. That would require you to do your data processing in PHP though.. probably not what you want. Or you could simply send back the data to your javascript.. but that'd be pretty awful to run an upload just to make the data available to JavaScript.

Categories

Resources