Skip to content

Read a text file on client side from c# .net

shaun tull

Asked Apr 2, 2008, 8:12 AM EDT1 answer
Share
LinkedInX

I have a web app that  when a button is clicked it will read a text file on the users c drive and display the results of the text file.

Java
StreamReader re = File.OpenText(@"c:\Datadetails\serverresults.bcp");
string input = null;
while ((input = re.ReadLine()) != null)
{
Label1.Text = (input);
}

I have used the  above code and when the button is clicked it displays data from the servers c drive only. I need client side code that can read a file on the users local c drive and display it to the user,

The code below is a javascript function that is suppose to get the data client side  but I cannot get this to work when a button is clicked. Does anyone know how I could place this in a button control to get this working.

Thanks for any help if anyone knows how to resolve this

 

 

JavaScript
function ReadFromFile() {
var strContent = ReadFileToString(document.Form1.TxtFileName.value);
document.Form1.HdnContent.value = strContent;
document.Form1.submit();
}
function ReadFileToString(strFileName) {
var strContents;
strContents = "";
objFSO = new ActiveXObject("Scripting.FileSystemObject");
if (objFSO.FileExists(strFileName)) {
strContents = objFSO.OpenTextFile(strFileName, 1).ReadAll();
}

return strContents;

 

I tried placing this in  Page.ClientScript.RegisterStartupScript but could not work out the correct syntax.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    You have to make sure the following:

    Your browser should have access to run ActiveX objects. If receive a error message like "Automatic Server Can't Create Object" you most go to Internet Option and in security tab select custom level in Local part and change options to get effect.

    ActiveX objects will work only in IE, i guess.

    0