473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Parsing an uploaded text file

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.

Dec 21 '06 #1
6 1977
Should have mentioned above, I have to do this using the 1.1 framework.

Dec 21 '06 #2
"Greg" <ge******@aol.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
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/lib...putstream.aspx
Dec 21 '06 #3
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;
}

--
Milosz Skalecki
MCAD
"Greg" wrote:
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.

Dec 21 '06 #4
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

Dec 21 '06 #5
"Greg" <ge******@aol.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
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...?
Dec 21 '06 #6
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

--
Milosz Skalecki
MCAD
"Milosz Skalecki" wrote:
No probs
stream.Position = 0
--
Milosz Skalecki
MCAD
"Greg" wrote:
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
Dec 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tripredacus-X | last post by:
I am experiencings a temporary parsing issue on a server running Apache I am not sure of the version at this time. Here is what happens: I code the XML files and have it refer to an XSL, which in...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
3
by: Arsalan Ahmad | last post by:
Hi, I have recently uploaded a file in a webserver. When i try to access it via browser, I get following error: This is working fine in my local copy. Any idea what could be wrong. Server Error...
2
by: Brett | last post by:
I uploaded an ASP.NET website from my developer machine to a remote server. The paths differ slightly as follows: Local Path: C:\Inetpub\wwwroot\mysite Server Path: C:\Inetpub\mysite.com\ I...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
14
by: =?Utf-8?B?U2FtdWVs?= | last post by:
Hi, I have a web app that allows others to upload files, and the problem is that if I allow users to upload image files, fake image can be uploaded and cause XSS issues. In the app, I do...
22
by: JJ | last post by:
Whats the best way for me to pull out records from a tab delimited text file? Or rather HOW do I parse the text, knowing that the tabs are field delimiters and a return (I image) signifies a new...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
4
by: liberty1 | last post by:
Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help. PROBLEM NUMBER 1: Using PHP and MySQL, I am able to upload...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.