Edit a file using javascript - javascript

I need to edit an xml file using javascript. Now I'm involved in a project of online testing.
The question.xml file is already in the project folder.
In that i want to add or edit the questions(only using javascript). I'm able to bring the particular content through ajax, but I am not able to edit the file.

Javascript can't write to a file. The best you'll be able to do is get Javascript to read and edit the XML then post that data to a server-side script to write to file.

Until now, Google Chrome is the only web browser that has a functioning implementation of the FileSystem API, therefore, it may allow you to save files locally using only Javascript.
Obviously, for security reasons, when writing files to the local file system, the user must explicitly allow it.
A working tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/

Nickf is correct. The reason Javascript can't write to a file is because it is a Client-Side language. Javascript will never have permission to write a file because it has to operate inside the browser sandbox.
You will need to use a server-side script (.NET, PHP, ColdFusion, etc) to write the file.

If you are willing to use Google Gears, you get a sandbox on the client machine on which you can write files.

Javascript has no built-in file I/O (a.k.a. you can't do it with JS alone)
Instead use some kind of server side language such as PHP or ASP.NET in conjunction with Javascript's AJAX functionality.

Look over Adobe's Flex development system. There are ways you can use it to build an app that runs in the browser (or not) and can access the filesystem (Windows/Mac/Linux). It's programmed in ActionScript, a dialect of javascript; and can interoperate with javascript in the browser.

Related

Read properties file from local machine using javascript/jquery/angular js/angular

We have a properties file in local machine (for example, c:/foo/foo.properties).
How to read that properties file to get the content in it using javascript, jquery and angular?
Please provide a solution to read it, only using javascript, jquery
and angular.
It's an urgent requirement.
The answer is: It's an impossible mission. There is no way for you to read a file on a computer using Javascript or anything of its frameworks or libraries, except one: which is NodeJS.
You cannot access a local file using client side script due to security issue you have to use some server-side script like node.js,php etc for that to work.

Writing to a System File Using Javascript

I'm working with an API with a feature that can only be accessed (easily) using Javascript, but I want to use the API to save a .txt file to my server. Is there any way to achieve this on a Mac OSX machine? I know that JS running in a browser is prohibited from doing this, so I guess this is really a two part question: (1) what's the simplest way to run a 10 line JS script on Mac OSX and (2) how would I write data to a txt file doing this?
You could go for a headless browser for example PhantomJS. I haven't used it, but it should run JS well.
The same restrictions apply though, no filesystem access from JS.
Unless you use something like the plugin framework in FF, in that case you have elevated rights for the scripts.
Besides that you could create a wrapper in php, perl or other language, and pass on the data from JS to them in an ajax call, and they write the txt for you.

How can you read, write, and create ".txt" files using Javascript?

I am writing a website and I have a very basic understanding of JavaScript (JS) but a good understanding of HTML and CSS. I want to have an admin part of the website were you can edit the content of the site. I also need help on how to display these text files onto the website using JS. Thank you for your help.
No. You can't. For this you will need backend server software, like PHP.
You can do it with ActiveX objects - http://www.yaldex.com/wjscript/jsfilecreateTextFile.htm - but you're tying yourself to proprietary IE support.
Other than that, you'll need a server-side language such as PHP.
In HTML5 you can operate on local files via the File API (have a look at http://www.html5rocks.com/en/tutorials/file/dndfiles/ for a quick tutorial) but manipulating files on the server using JavaScript on its own is impossible. You need to use AJAX to send a request to a PHP (or some other server-side language) script to do it for you.
Impossible, only server-side languages

Is it possible to convert a mp3 file to m4r with Javascript or jQuery?

Is it possible with Javascript or jQuery to convert mp3, wav, etc. to m4r format?
Let's assume you had a library that can change the format of files.
Let's also assume you only need the application to work on current browsers that implement FileAPI or FileReference so you can have access to uploaded files (you can't have access to them without FileAPI or FileReference unless you use Flash or Java Applets or equivalent technologies).
You wouldn't be able to write the output file back to the user because JavaScript is not allowed to access the local filesystem.
Your only solution would become sending the converted file to the server and the server sending it back to you with a force download directive so that the user will be prompted to download the results.
Now back to if there were a library that can the conversion (or even native JavaScript)... I haven't heard of any. It's not impossible to build one but it is impractical and wouldn't run very fast.
Edit:
Let's not forget Node.js!
It's a backend server that uses Google Chrome's V8 JavaScript interpretor/compiler. And it runs JavaScript as a backend scripting engine.
You have access to filesystem, databases and everything if you use that (or any other backend system for that matter) and still be using JavaScript. You can use libraries too. Either written in JavaScript or libraries written in other languages that have been linked to interface with Node.js.
Edit 2:
There is a PC emulator written entirely in JavaScript. It runs binary executables if you want it to. It's called JSLinux.
If you're feeling particularly rambunctious you can grab the ffmpeg binary executable (compiled with static linking). And embed it into your application code as an uuencoded string then use JSLinux to execute the commands and grab the results.
Indeed, it is possible doing this on the client using the latest js technologies. A web-worker thread can do the work in the background. At least in Firefox and Chrome it is also possible to read ("upload in memory") and write ("download from memory") files using the new W3C File API, see here.
I managed to read files via drag&drop from and within the client using google's GWT which in the end is plain javascript, so it must also be possible to do it "natively".
Besides that, the conversation algorithm of course has to be implemented in a javascript web worker to avoid blocking the gui. This should be the hardest part, but not impossible, though.
You would need a backend to do this. You may want to look into the PHPExtension of FFmpeg

Writing to a file

I am extracting content from a web page using JS/JQuery and want to write the data to a comma separated file. I know javascript doesn't really support file io and I'm trying to avoid setting up a local apache server just to process the content to a file. Is there an easier way to do this?
You can have your JS create the file text in a string. Then open a new window and write the string into the new window.
You can write a Mozilla Extension.
PROs:
You can use JS/JQuery on the extension
You can write files on the extension
CONs:
You can only use the application in Firefox (or any other Mozilla based browser).
There is no simple way of doing this. W3C has recently published the first draft of a File API that will make it possible, but it'll be a while until this is through and widely implemented.
Still requires a webserver, but you could build a small one method web service that your js calls, then the service can write out the data.

Categories