How do I read a text file ?

Hi,

I have a ACtiveX control and In that I have a method called, GetFileContents(str as string). This method takes string and uses this to initalize the control with that string.

I am hosting this activeX control on a web page and in the on_load event of the web page, I need to read contents of a ASCII text file and pass that to the ActiveX control using the exposed method GetFileContents.

This file will be there on the server will be there in virtual directory (where my default.asp) exists.

Can some tell me a way for doing this ?

Thanks!
Gangisetti
LVL 1
gangisettiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
Dim sFileName as String = Server.MapPath("Whatever.txt")

Dim oStreamReader as StreamReader
oStreamReader  File.OpenText(sFileName)

Dim sContents as String = oStreamReader.ReadToEnd()

Now you have file contents in sContents variable.
amit_gCommented:
You will have to import System.IO either by

<%@ Import Namespace="System.IO" %>

if you don't use code behind or by

Imports System.IO

if you use code behind.
gangisettiAuthor Commented:
amit,

I have tried doing this.

But  the problem is, when the script is getting executed on a client machine,

The server.mapppath is getting resolved as the absoulte path of the server machine. I have placed the text file in
a location called E:\work\myASp\myfile.text.

When the script executes, its running with some errors.

When I do a View Source on that page on the client sides, I am able to see the script getting changed as E:\work\myAsp\myfile.text.

I hope you understood the problem.

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

amit_gCommented:
The Server.MapPath is to get the absolute path and so it is doing what it is suppoesd to do. What error does it return?
gangisettiAuthor Commented:
Absoulte path in the sense, it should get the path corresponding to the server right!!, it should be some thing like

if i am running my srcipts on this path, http:\\cobra\myscripts

then for reading the text, file it should be looking at the same path, http:\\cobra\myscripts\myfile.txt. It should not refer to a drive letter, if it does so, how will the script running on the client know about E:\ or D:\ ?

I think my script is running at the client side, How will I make it to run at server side ?
amit_gCommented:
Page_load event runs on the server not on the client and so the path must also be from server's point of view. Since the file is on the server, it has to be opened like E:\...\ by the server script. Where is your ActiveX loaded? On the client or on the server? What error do you get? Post some code that shows how you are loading the ActiveX.
gangisettiAuthor Commented:
My activex is loaded on the client only. Once it is downloaded to the client, then it runs.

This is the way I am launching the ACtiveX

<OBJECT ID="MyCtr.MyInterface" name = "ctrl1" style="Z-INDEX: 101; LEFT: 30px; POSITION: absolute; TOP: 5px" WIDTH="820" HEIGHT="600" CLASSID="clsid:FAD9DEB7-8D4C-4F47-AD67-9B4F2549F8AD"
CODEBASE="MyCtrl.CAB#version=1,0,0,0" >
amit_gCommented:
Ok. Do a little test. In the code posted above add a line

Response.Write sContents

Run the aspx and check if you can see the file contents on the browser. If you can, the file is getting read properly. Now to send it to the server we will need to take help of some tag that is hidden from the user but is still there on the page. There are many ways it can be done. Confirm the result of this test and we will do the next step after that.

Post one more thing from your page. How are you calling the method on the ActiveX? It has to be done in JavaScript. How are you doing it now? Post that too along with the result of the test.
asafmmCommented:
Your problem is a client server issue.

You have a file on the server.
You have an object on the client/

How will the client object access the server ? by passing the file content as everything is passed from server to client in the web - HTTP.

DO THIS :
Insert a small piece of code to your server side script (very small script - you can put 10 lines no more).
The above addition will produce the file content within a hidden SPAN in the page, and then a small script in the client side will take the innerText of the SPAN into your object.

The server side script will generate HTML on the fly that will look like this :
<SPAN ID="TextFileContent" NAME="TextFileContent">
.....
.....
</SPAN>

The client side script will be :
GetFileContents(TextFileContent.innerText)

It is a bit tricky but ASP is tricky.
Listen - if you are running this app on an intranet then you can find easier solutions like connecting to the IIS server as it was file server and many more things can be done on an intranet.

And one more thing - if you develope the object then one more solution is to execute an ftp command and fetching the file to the client and then read it loccally/

Thanks
Asaf
Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT PlanningCommented:
set fso = Server.Createobject("Scripting.FileSystemObject")
path = "E:\work\myASp\myfile.text"
set file = fso.opentextfile(path, 1)  <-- For reading

strText = file.readall

set file = nothing%>


on the client side in a script tag after creating an activex object instance MyActvX

MyActvX.GetFileContents = <%=trim(strText)%>
gangisettiAuthor Commented:
Sorry Silvers5.

whatever you suggested is not working!! In your snippet, you have declared
"strText" where it needs to be declared ? when i pass that as a string, my activeX control method is getting a empty string.


asafmm. can you tell me more about that SPAN ? i havent understood that clearly..
amit_gCommented:
Could you sucessfully read file on the server? If yes, you can move to the next step. As I said there are several ways you can pass the contents to the client and in turn to the ActiveX using JavaScript and using span is one of those methods. But before you go there, you need to read the file and get contents first.
Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT PlanningCommented:
amit wrote the .net version correctly.. didn't notice it was for .net as the default.asp mislead..

can you post your code behind?
Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT PlanningCommented:
why not to store the data needed in the config file? and you will get it

in your web config add this for example:

  <appSettings>
    <add key="TestIt" value="read this !" />
  </appSettings>

from your code you simply get the data like..
        Dim TestIt As String = ConfigurationSettings.AppSettings("TestIt")
<%response.write(TestIt)%>  where needed

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.