Writing data to a local file using HTML5 and Javascript in Chrome - javascript

I have created an interactive quiz using html and javascript which will be run on a touchscreen at an event and I need to write the results to a local csv file (so no internet connection). It needs to write to an already existing file, so it cannot be done where the data is stored locally and a download link is generated through the browser.
How would I go about doing this? All methods I have found are either unreliable or no longer supported. The browser I am using is Chrome, so it does not need to be cross-browser compatible.
Can anybody help or point me in the right direction please?

Install a web server.
Point the browser at http://localhost.
Send the data to the server using Ajax or a form submission.
Process the data (including storing it in a file) using the server side language of your choice.

When javascript is used only in the client-side cannot write data as you want.
Follow the #Quentin recommendation about install some web server, as apache using php for instance(It is pretty straight forward!). I also recommend you to create restFull methods to do it with jquery calls from the client side, it is easy to find many examples in the internet and quicky...
If you want something more easy you could work with html post using forms in php, the most easy way to do it.

Related

Is there any way to edit a local file through a web page without running server software?

I've a web working on a web page that's basically just a big table of links. I use javascript to read from a text file, parse it, and create a table based on that.
I'd like to be able to have a button on the page to add new a row of links and add them to the text file (or another file type if it's better).
I know you can accomplish this with php, node.js, and others, but all the methods I've found require server software to be running. Is there any way around this? For example, is there a way to use javascript to call a python script, or any other way?
The page is just for personal use, so I'd like to avoid running server software just to use it if possible. I know you can set it to download a text file, and you can save it in the same location, but I'd also like to avoid that.
From the research I've done, it doesn't seem possible, but I just thought I'd ask before I give up. Thanks in advance.
You can only read from files locally in browser with javascript.
This would be a huge security vulnerability if scripts in browsers could write files to your machine.

In-browser conversion of MS Word document to PDF

I would like to implement an in-browser Microsoft Word document merge feature that will convert the merged document into PDF and offer it to the user for download. I would like to this process to be supported in Google Chrome and Firefox. Here is how I would like it to work:
Client-side JavaScript obtains the Word template document in docx format, either from a server, or by asking the user for a file upload (which it can then read using the FileReader API)
The JavaScript uses its local data structures (e.g., data lists it has obtained via Ajax) to expand the template into a document. It can do this either directly, by unzipping the docx file and processing its contents, or using DOCx.js. The template expansion is just a matter of substituting template variables with values obtained from the local data structures.
The JavaScript then converts the expanded template into PDF.
The JavaScript offers the PDF file to the user for download, e.g., using Downloadify.
The difficulty I am having is in step 3. My understanding (based on all the Googling I have done so far) is that I have the following options:
Require that the local machine is a Windows machine, and invoke Word on it, to convert to PDF. This can be done using a little bit of scripting using WScript.shell, and it looks doable with Internet Explorer. But based on what I have read, it doesn't look like I can call WScript.shell from within either Chrome or Firefox, because of their security constraints.
I am open to trying Silverlight to do the conversion, but I have not found enough documentation on how to do this. Ideally, if I used Silverlight, I would like to write the Silverlight code in JavaScript, because (a) I don't know much CSharp, and (b) I think it would be much easier in JavaScript.
Create a web service that will convert a given docx file to a pdf file, and invoke that service via Ajax. I would rather not do this, if possible, for a few reasons: (a) I tried using docx4java (I am a reasonably skilled Java programmer) but the conversion process is far too slow, and it does not preserve document content very well; and (b) I would like to avoid a call out to the network, to avoid security issues. It does seem possible to write a little service on a Windows server for doing the conversion, and if there is no other good option, I might go that route.
If I have been unclear about anything, please let me know. I would appreciate your ideas and feedback.
I love command line tools.
Load the doc to your server and use LibreOffice to convert it to PDF via the command line
soffice.exe --headless --convert-to pdf --outdir E:\Docs\Out E:\Docs\In\a.doc
You can display a progress bar to the user and when complete give them the option to download the doc.
More info on LibreOffice's command line parameters go here
Done.
Old old question now, but for anyone who stumbles across this, web assembly (wasm) now makes this sort of approach possible.
We've just released https://www.npmjs.com/package/#nativedocuments/docx-wasm which can perform the conversion locally.

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 text file using Javascript

I want to write to a text file using javascript. I know that it is possible but there are some problems.
I am running a javascript program that calculates the location of an object (its latitude & longitude) which changes every 5 seconds; i want to write this information to a text file. The javascript program will soon run on a server and I'll use the information written to the text file to communicate with an Android app on my phone.
So, my question really is:
How can it be done properly?
I know that there may be some permission issues but considering it won't be online and available to others will it be a problem and, if it is, should I go with PHP to do what I want? I know ASP is more Microsoft orientated and I work with a Mac so PHP would be the preference here.
When writing a file, is it possible to write to an existing file or does the process simply destroy and recreate the same file?
I would use PHP
http://www.php.net/
This has a good code example:
http://www.tizag.com/phpT/filewrite.php
Also, you can make the request using jQuery's AJAX function, this will allow to effectively run this code from javascript:
http://api.jquery.com/jQuery.ajax/
Using the latest HTML5 Javascript, you can use the FileSystem APIs to read/write/append to text files. This is a good tutorial here : Exploring the FileSystem APIs.

Edit a file using 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.

Categories