Parsing an uploaded text file

G

Greg

I'm at my wits end on this one...I have an aspx page that allows the
user to upload a text file. Then I'm saving the file to a binary
column in a sql database. However, I need to be able to parse this
text file. I know I can simply spit the file out to the hard drive and
then open it using a filestream, but it seems like a waste of resources
to write the file, read it, parse it and then delete the file.

Can anyone tell me how to simply take the file.postedfile.inputstream
and read the text line by line? I have to admit that I'm not very well
versed using streams, so a simple example would be great.

Thanks.
 
M

Mark Rae

I'm at my wits end on this one...I have an aspx page that allows the
user to upload a text file. Then I'm saving the file to a binary
column in a sql database. However, I need to be able to parse this
text file. I know I can simply spit the file out to the hard drive and
then open it using a filestream, but it seems like a waste of resources
to write the file, read it, parse it and then delete the file.

Can anyone tell me how to simply take the file.postedfile.inputstream
and read the text line by line? I have to admit that I'm not very well
versed using streams, so a simple example would be great.

Sounds like you need the InputStream property of the HttpPostedFile object:
http://msdn2.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx
 
G

Guest

Howdy,

Here's an example:

protected void btn_Click(object sender, EventArgs e)
{
if (fup.HasFile)
{
parsingResult.Text = ParseContent(fup.PostedFile.InputStream) ?
"passed" : "not passed";
}
}

private bool ParseContent(System.IO.Stream stream)
{
using (System.IO.StreamReader reader =
new System.IO.StreamReader(stream, System.Text.Encoding.ASCII))
{
string line;

while ((line = reader.ReadLine()) != null)
{
if (line.IndexOf("greg", StringComparison.InvariantCultureIgnoreCase) !=
-1)
return true;
}
}

return false;
}
 
G

Greg

Shew...finally. Thanks Milosz. I converted your code to VB and now
have everything working. I had already attempted this in much the same
fashion, but all I ever got was all '0's or '-1's. Come to find out,
this was due to code saving the file to the sql server first. Now I
just need to figure out how to 'reset' the stream back to the
beginning.

Mark- I appreciate your input as well. I had found the link that you
provided earlier, but when I tried the code supplied by MS, the
resulting string was just a series of numbers, no text.

Thanks to both of you!

Here's the code that I used:

Private Function ParseContent(ByVal MyStream As System.IO.Stream)
As String
Dim Reader As New System.IO.StreamReader(MyStream,
System.Text.Encoding.ASCII)
Dim strLine As String
Dim strFile As New System.Text.StringBuilder()
Do While True
strLine = Reader.ReadLine
If strLine Is Nothing Then
Exit Do
End If
strFile.Append(strLine & Chr(13))
Loop
Return strFile.ToString
End Function
 
M

Mark Rae

Mark- I appreciate your input as well. I had found the link that you
provided earlier, but when I tried the code supplied by MS, the
resulting string was just a series of numbers, no text.

Did you forget to set the TextEncoding...?
 
G

Guest

Sorry,

I thought you want to move to the begining of the stream. Your problem is
encoding, in my example i used ASCII encoding, but the file(s) you're
uploading have Unicode or different one.

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top