Thursday, November 12, 2009

Read files through Javascript

I Came across a scenario, i.e, as follows


1) An ASP.Net application is configured in a server.
2) I need to open a file from client side in a text area(the file can be any type)
3) I can modify it
4) I need to save the modifications in the same path of client


Here we can’t open the file in Fileupload control, since it uploads the file to the server location. The modified file can’t be save in the clients location.
So, I went for Javascript. I just copy a code for illustration

<form id="form1" runat="server">
<div>
<input type=text id="txtFile" /><input type="button" id="btnSub" value="Open" onclick="ReadFiles();" />
</p>
 <textarea id="txtFileData" name="S1"></textarea></div>
</form>


<script>
function ReadFiles()
{
var txt=document.getElementById('txtFile').value;
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile(txt, ForReading);
s = ts.ReadLine();
while(!ts.AtEndOfStream)
{
s=s + ts.ReadLine();
}


document.getElementById('txtFileData').value=s;
ts.Close();
}

</script>

But here the Browser settings should enable the activeX objects.

No comments:

Post a Comment

...

Obstacles are those frightful things you see when you take your eyes off your goal.------> by Henry Ford