Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2006, 04:31 AM   PM User | #1
coolrunning
New to the CF scene

 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
coolrunning is an unknown quantity at this point
Read a text file in ASP.NET

Hi! Thanks and please give me some ideas on this:

I want to make a Web page, with two windows (such as 2 textareas). One is for user to enter an asp .net file, such as
<html>
....
</html>

The other is to display the result as in a browser.

I am using VB .NET to write this. I am thinking
1. I should read the textarea content into a ~.aspx.
2. Then display it in an iframe by using src=~.aspx

Should StreamReader and StreamWriter be introduced? Or there should be a better way?

So do I need to make sure that the server that I am uploading this file to can interpret ASP.NET?

Thank you so much for your input.
coolrunning is offline   Reply With Quote
Old 08-18-2006, 08:23 PM   PM User | #2
coolrunning
New to the CF scene

 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
coolrunning is an unknown quantity at this point
Here is what I have already done. What's wrong?

If anyone can shed some light on what I have already done. Right now what's being changed by user in one window, is not registered. Thanks.

This is ASP .NET, written in VB .NET

Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page

Dim fileStreamReader As StreamReader
Dim newfileStreamReader As StreamReader
Dim i As Integer = 0
Dim j As Integer = 0
Dim fileStreamWriter As StreamWriter
Dim aString As String
Dim oldFile As String = "old.htm"
Dim newFile As String = "new0.htm"


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Read_Write_Record()
End Sub
Private Sub Read_Write_Record()
With Me
.contentLabel.Text = ""
.TextBox1.Text = ""
.Label1.Text = "oldFile is " & oldFile
.Label2.Text = "newFile is " & newFile
.Label5.Text = ""
End With

aString = ""

Try
fileStreamReader = New StreamReader(Server.MapPath(oldFile))
j = j + 1
fileStreamWriter = New StreamWriter(Server.MapPath(newFile))
i = i + 1
While fileStreamReader.Peek <> -1
aString = fileStreamReader.ReadLine()
fileStreamWriter.WriteLine(aString)
With Me
.TextBox1.Text = .TextBox1.Text & aString & ControlChars.CrLf
.contentLabel.Text = .contentLabel.Text & aString
End With
End While
Catch ex As Exception
Me.contentLabel.Text = "File doesn't exist!"
Me.Label5.Text = "i = " & i & "j = " & j
End Try
fileStreamReader.Close()
fileStreamWriter.Close()
oldFile = newFile
newFile = "new" & i.ToString & ".htm"
With Me
.Label3.Text = "oldFile is " & oldFile
.Label4.Text = "newFile is " & newFile
End With
End Sub

Protected Sub ClickButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClickButton.Click
Read_Write_Record()
End Sub
End Class
coolrunning is offline   Reply With Quote
Old 08-30-2006, 12:07 AM   PM User | #3
otaku149
Regular Coder

 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
otaku149 is an unknown quantity at this point
How to read your *.txt file:
Code:
Dim FileName As String = Server.MapPath("yourfile.txt")
Dim FileContents As String
Dim myStreamReader As StreamReader

If Not FileName Is Nothing Then 
	Try
		myStreamReader = File.OpenText(FileName)
		FileContents = myStreamReader.ReadToEnd()
		lblContent.Text = FileContents
	Catch myException As Exception
		Response.Write("Error...")
	Finally
		myStreamReader.Close()
	End Try
End If
How to write in your *.txt file:
Code:
Dim str As String = "bla blah..."
Dim sw As StreamWriter
Try
	sw = File.CreateText(Server.MapPath("yourfile.txt"))
	sw.Write(str)
Catch myException As Exception
	Response.Write("Error...")
Finally
	sw.Close()
End Try
__________________
Martial
http://getElementById.com
otaku149 is offline   Reply With Quote
Old 08-30-2006, 02:40 PM   PM User | #4
coolrunning
New to the CF scene

 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
coolrunning is an unknown quantity at this point
Otaku149,

Thank you! It is really cool that you replied. I started thinking no one would. I can't wait to try it.

I have another question. My design is to have 2 windows. I want to display a text file in the left window, and to display it as a browser would to the right window. My current code works:

With Me
...
.contentLabel.Text = .contentLabel.Text & aString
End With


But I don't really know why. Is it because the ".contentLabel.Text & aString" is naturally being displayed in a browser? I am using VB .NET to write ASP .NET.

Thank you so much again!
coolrunning is offline   Reply With Quote
Old 08-30-2006, 03:03 PM   PM User | #5
otaku149
Regular Coder

 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
otaku149 is an unknown quantity at this point
coolrunning,

I thought you wanted to create/read a ~.txt file and after reading your original post it is important to mention that what you want to do is dangerous. Allowing anyone to run an ~.aspx is very dangerous because he can run server code and for example he can delete all your website or something.
__________________
Martial
http://getElementById.com
otaku149 is offline   Reply With Quote
Old 08-30-2006, 06:26 PM   PM User | #6
coolrunning
New to the CF scene

 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
coolrunning is an unknown quantity at this point
Thanks Otaku149

Dear Otaku149,

What I am trying to do is to create a tutorial for my beginning students for JavaScript and ASP.NET. So the left window will be the text file code, such as

<html>
<body>
<script>document.write("<h1>This is a test.</h1>");</script>
</body>
</html>

And the right hand window would display

This is a test.

User can change the text file to make it display differently.

If the left hand side has ASP .NET file, I just want it to be displayed as a server-processed file, but not to endanger the system, any way to avoid it?

Thanks so much!
coolrunning is offline   Reply With Quote
Old 08-30-2006, 07:05 PM   PM User | #7
otaku149
Regular Coder

 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
otaku149 is an unknown quantity at this point
I understand exactly your goal coolrunning but it's too dangerous because you want to let anyone run server side script. People will be able to read, write and delete your website files. You give to others the same permission as you have. Believe me, do not do that except if you want to be hacked.
__________________
Martial
http://getElementById.com
otaku149 is offline   Reply With Quote
Old 09-09-2006, 12:32 AM   PM User | #8
coolrunning
New to the CF scene

 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
coolrunning is an unknown quantity at this point
Output

Thanks Otaku149.

Can you or someone tell me:

This is a 1 row 2 cols table. In the left cell, I use a textbox for input. In the right cell, I just let the browser do the displaying (rendering) of the text.

I find the dispaly area (the window to the right) get longer and longer, if the user enters a lot of text in the input area (the window to the left). The left window is a textbox, with a nice scrolling bar. The right window, unfortunately, just keeps growing as the input on the left window increases.

I tried frameset, but the ASP .NET in VB .NET did like it. I want to use textarea, which will give me the automatic scrolling bar. But I can't get the display like I had before with the regular table cell.

How do I get it, the right window, to display as it already has, as a browser, but has a scrolling bar when it gets too long? Thanks so much for looking into this.
coolrunning is offline   Reply With Quote
Old 07-08-2010, 12:16 PM   PM User | #9
robertsams23
New Coder

 
Join Date: Mar 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
robertsams23 is an unknown quantity at this point
Read Text File

You can read it very simple way in vb.net

http://vb.net-informations.com/files...TextReader.htm

thanks
rbt.
robertsams23 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:53 AM.


Advertisement
Log in to turn off these ads.