Saturday, March 24, 2012

Newbie: text files

Sorry if this is a simple question. I have a bunch of text files that I am
allowing I want to display on my web page. When I do a response.writefile
to load the file the CRLFs don't work. How can show the contents of the
text file to the user easily?

Thanks
ChrisA CRLF does not render in the broswer as a break/line feed, you'll have to
read the file into a string var and replace crlf with "<BR>"... HTH

"Chris" wrote:

> Sorry if this is a simple question. I have a bunch of text files that I am
> allowing I want to display on my web page. When I do a response.writefile
> to load the file the CRLFs don't work. How can show the contents of the
> text file to the user easily?
> Thanks
> Chris
>
Dim sr As New System.IO.StreamReader("pathToFile")
response.write(sr.ReadToEnd)

"Chris" <NoSpam@.NoSpam.com> wrote in message
news:%23jD9X6bKFHA.132@.TK2MSFTNGP10.phx.gbl...
> Sorry if this is a simple question. I have a bunch of text files that I
> am allowing I want to display on my web page. When I do a
> response.writefile to load the file the CRLFs don't work. How can show
> the contents of the text file to the user easily?
> Thanks
> Chris
This solution has the same problem as using response.writefile. The CRLF
fields don't display.

Chris

"Scott M." <NoSpam@.NoSpam.com> wrote in message
news:uDBJJBcKFHA.3484@.TK2MSFTNGP12.phx.gbl...
> Dim sr As New System.IO.StreamReader("pathToFile")
> response.write(sr.ReadToEnd)
>
> "Chris" <NoSpam@.NoSpam.com> wrote in message
> news:%23jD9X6bKFHA.132@.TK2MSFTNGP10.phx.gbl...
>> Sorry if this is a simple question. I have a bunch of text files that I
>> am allowing I want to display on my web page. When I do a
>> response.writefile to load the file the CRLFs don't work. How can show
>> the contents of the text file to the user easily?
>>
>> Thanks
>> Chris
>>
"Chris" <NoSpam@.NoSpam.com> wrote in message
news:eLb9fIcKFHA.3916@.TK2MSFTNGP14.phx.gbl...
> This solution has the same problem as using response.writefile. The CRLF
> fields don't display.
> Chris
> "Scott M." <NoSpam@.NoSpam.com> wrote in message
> news:uDBJJBcKFHA.3484@.TK2MSFTNGP12.phx.gbl...
>> Dim sr As New System.IO.StreamReader("pathToFile")
>> response.write(sr.ReadToEnd)
>>
>>
>> "Chris" <NoSpam@.NoSpam.com> wrote in message
>> news:%23jD9X6bKFHA.132@.TK2MSFTNGP10.phx.gbl...
>>> Sorry if this is a simple question. I have a bunch of text files that I
>>> am allowing I want to display on my web page. When I do a
>>> response.writefile to load the file the CRLFs don't work. How can show
>>> the contents of the text file to the user easily?
>>>
>>> Thanks
>>> Chris
>>>
>>
>>

Dim sr As System.IO.StreamReader = New
System.IO.StreamReader("TextFile.txt")
Response.Write(sr.ReadToEnd().Replace(vbNewLine, "<br>")

Hope this helps :)

Mythran
As well as replacing the line breaks with <br /> tags you will want to
Server.HtmlEncode all of your text. Not doing so can cause security problems
if you allow people to upload files. Even if you don't allow people to
upload files, Html encoding the text is a good idea so that everything
displays correctly.

0 comments:

Post a Comment