Writing to a file - javascript

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.

Related

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.

Can you do file IO using Javascript without ActiveX

Can you do file input/output using Javascript without using ActiveX, submitting a form to a server, or other extensions?
The reason I ask is after hearing all about HTML5 I thought I might take the time to find out what all the hype is about but I can't see any way to create regular applications.
An example of what I thought I might be able to do would be to create a simple text editor in the browser using only HTML5 and Javascript, where I can open and save files from the disk. After that I was thinking to try and create some kind of icon editor using the canvas tag. But without any kind of file IO each example is rather pointless (in my opinion that is). So, is this possible? And if so how would I go about doing it?
Thank you!
Yes. See this Mozilla demo which extracts EXIF data from a local image. The W3C is developing an API.

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