7

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.

3
  • Can you explain why you do not want to save the file on the server? If you do that, it is very easy to load the CSV file with for example this codeproject CSVreader Commented Feb 21, 2011 at 10:00
  • What I want is to read the file in the client side itself and extract the data and save the data into a database. I cannot be saving the files in the server because then I would be saving a lot of files.Is there any option?? Commented Feb 21, 2011 at 10:04
  • Save the file on the server in a temp directory (make sure to make the filename unique), read it into the database, delete the file... Commented Feb 21, 2011 at 10:40

3 Answers 3

4

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;
                }
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any good jquery pulgin to read the file as a stream and senditoer to a server??
Can anybody help me with this issue??
@sunshine - Please start new question for this.
2

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)

Comments

0
    private async Task<string> ProcessAsync(string surveyId)
    {           if(!Request.Content.IsMimeMultipartContent())
        {
            return "|UnsupportedMediaType";
        }

        try
        {
            var provider = new MultipartMemoryStreamProvider();

            await Request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
            await Request.Content.ReadAsMultipartAsync(provider).ConfigureAwait(false);

            HttpContent content = provider.Contents.FirstOrDefault();

            if(content != null)
            {
                Stream stream = await content.ReadAsStreamAsync().ConfigureAwait(false);

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

                    while ((inputLine = CsvReader.ReadLine()) != null)
                    {
                        string[] vars = inputLine.Split(',');


                    }
                    CsvReader.Close();
                    //return values;
                }

            }
        }
        catch(Exception e)
        {
            return e.ToString();
        }

        return "Nothing To Process";
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.