Processing Files

  • Thread starter Thread starter Shahid
  • Start date Start date
S

Shahid

Hello,

I am having a problem with users uploading a file. Basically the way my
application works is that a user will select a file from the system
(csv) and I will take that file and do some processes on it. I am not
saving the file or making any changes to it.

Once I try to parse the file, I get the following error:

Exception Details: System.IO.FileNotFoundException: Could not find file
"C:\WINDOWS\system32\file.CSV".

notice that the path given is C:\windows\system32. but i had selected
file.csv (which exists) from another directory (c:\file.csv).

The place where it crashes on the code is:

using(StreamReader sr = new StreamReader
(inputFile.PostedFile.FileName)
{
string line = null;
int ln = 0;
while((line = sr.ReadLine()) != null)
{
string[] fileFields = splitRx.Split(line);
ln++;
fields.Add(fileFields); //Save all the data for later
}
}

Thank you in advance for your help
Shahid
 
Shahid,

How do your users select the file to be processed? Are you using an
OpenFileDialog? If so, you should be able to programmatically get the
path of the file. Also, look into the DirectoryInfo class to set the
path of a file. Let me know if this helps, if not I have some sample
code somewhere on my harddrive I could find and paste in for you.

~ Justin
 
Hi Justin,

Thank you for response. I have this for my file dialog - <INPUT
class="box1" id="inputFile" type="file" name="inputFile"
runat="server">

If you can provide me with sample code, it would be greatly
appreciated.

Thank you,
Shahid
 
Shahid,

I'm not sure if I totally understand what you're doing. Is this a
webservice? Please give some details about the project.
 
It is a webapplication using C#.

I just need to go through the file that the user provides so I can take
the data and store it on the database.
 
Shahid,

I'm sorry, I have little to no experience with webservices, so I don't
know what is different and what is the same as programming local
applications. I would suggest posting to the
microsoft.public.dotnet.framework.webservices board. You should find
what you need there. Good luck.

~ Justin
 
Shahid,
If you want to work with an uploaded file you need to save it to the
filesystem at the location you want first.

HttpPostedFile myFile = inputFile.PostedFile

myFile.SaveAs( Server.MapPath("files/"+myFile.FileName);
using StreamReader sr= new
StreamReader(Server.MapPath("files/"+MyFile.FileName)
.... etc
that's untested code, but I think it gets the idea across.

I think you can also work with the file contents in memory without saving
it, if that's your preference.
Peter
 
inputFile.PostedFile.FileName will contain the name of the file as it
existed on the client machine. The only reason that it's even available
is just for some added info in case you want to name the file the same
on the server, etc. What you actually want to use to accomplish this is
....

using(StreamReader sr = new
StreamReader(inputFile.PostedFile.InputStream))

Bruce Dunwiddie
http://www.csvreader.com
 
Back
Top