Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload and read a CSV file in ASP.NET MVC3

I am working on a project and I need to upload a CSV file and read it. I am working in Visual Studio 2010 and MVC3 and C# language.

If I am to use html fileuplaod control, how I am suppose to take the uploaded file and read it in the client side itself without saving the file in the server. Do I have to use the jquery? I have searched but did not get solution to meet my requirements. I am new to MVC3 and CSV file handling and quite confused.

*What is the easiest way to upload a .csv file and read it in order to save it in the database.

A clear solution would be highly appreciated.Thanks.

like image 498
Umesha Gunasinghe Avatar asked Feb 21 '11 09:02

Umesha Gunasinghe


2 Answers

What you can do is save the file on server, then after you read the content from them you can delete the file.

I think there is a no way you can read the from client side. You must upload it on ur server to read that.

using (StreamReader CsvReader = new StreamReader(input_file))
                {
                    string inputLine = "";

                    while ((inputLine = CsvReader.ReadLine()) != null)
                    {
                        values.Add(inputLine.Trim().Replace(",", "").Replace(" ", ""));
                    }
                    CsvReader.Close();
                    return values;
                }
like image 71
Bhavik Goyal Avatar answered Nov 10 '22 14:11

Bhavik Goyal


You should be able to access the data without saving it - using the InputStream property

http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx

and this (see Paulius Zaliaduonis answer)

like image 2
Jon Spokes Avatar answered Nov 10 '22 16:11

Jon Spokes