Thursday, May 29, 2008

Reading a file from local file system using javascript

Reading a file into javascript variable is a issue anyone can face while developing client side application.

The problem has different solution depending on platform/browser

Solution

1) Windows/IE
: Use the available FileSystemObject. It allows one to read and write files from local machine.

2) Firefox
: It does not have a object corresponding to FileSystemObject in IE. So we rely on XMLHTTPREQUEST Object provided. Our strategy here is to send the file to the server. On server read the file and flush the content to the client. On client side we can get the file contents in xmlhttprequest.responseText property.

3) Applet:
This solution makes use of Java Applet which allows you to read the local files.

Example of 2nd method using PHP as scripting language

1) From the client side write a form having a file upload utility.

2)Send the file to the server.

3) On sever side read that file and flush the contents

PHP Code

if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "
";
$file = fopen($_FILES["file"]["tmp_name"], "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "
";
}
fclose($file);
}

4) On client side read it as
var fileContent=xmlhttprequest.responseText


Thats it!! you are done.

Monday, May 26, 2008

Export to CSV + PHP

On Client
From the client send the string in csvContent field.
input name="csvContent " type="hidden"

Send the fileName in fileName field.
input name="fileName " type="text"

On the Server

$Content = $_POST['csvContent'];
$filename=$_POST['fileName'];
header('Content-Disposition: attachment; filename='.$filename);
header("Content-type: application/text");
echo $Content;

Set header :
1. Content-Disposition:attachment; filename='fileName'
2. Content-type:application/text

Write the content to the browser.

Result : It will open a save-as dialog on the browser.

Wednesday, January 9, 2008

WEB 2.0 : Reincarnation of web

Everyone related to web development use a word which is very difficult to define. The word is WEB 2.0.

What is it ?
Web 2.0 is a trend in the use of www and webdesign that aims to enhance creativity, information sharing, and, most notably, collaboration among users.

Web 2.0 is a marketing buzzword, which is made up of a group of buzzwords.
Let me try to define the buzzwords which combine and make WEB 2.0.

RIA
The most important component of web 2.0 is RIA ie Rich Internet Applications.
Now you might ask what do i mean by rich internet applications. RIA means web applications which have a rich user interface. All of us have used desktop applications in one form or the other.
Desktop applications have a rich user interface that comprises of fast response, great and good looking features like drag and drop etc. RIA brings all these features to web application's user interface. We have AJAX ie Asunchronous Javascript and XML calls for fast server side response and various toolkits which provide us ability to have all cool desktop features on our web page.
Some of the JS toolkits and libraries which provide development of RIA are DOJO, Prototye.js, Windows.js, Scriptaculous.js, Effects.js etc.

SOA - Service Oriented Atchitecture
Web 2.0 thrives on pulling information from different distributed and independent sources. RSS , ATOM feeds, REST calls, consuming wxposed services by various companies etc became the source of information which is presented on Rich UI. Web 2.0 Apps consumes a lot of information from these sources.

SocialWeb
Web 2.0 Applications promotes social networking and colloboration amongst users. A nice example of Web 2.0 is Pramati technologies product ShopprStream. It allows user to write reviews, comment on existing posts, telling people about what a user found interesting on net etc. The whole idea is to bring user in picture. Empowering user and make him feel that he is part and parcel of the game. Instead of just browsing websites now user can edit them. Social web is a big factor behind the success of web 2.0 among the users.

Finally summarizing all the discussion WEB 2.0 is combination of RIA , SOA , Social WEB.