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

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

If I use html fileuplaod control, then how can I take the downloaded file and read it on the client side itself, without saving the file on the server. Should I use jquery? I searched, but did not get a solution to meet my requirements. I am new to MVC3 and CSV file processing and am rather confused.

* The easiest way to download a CSV file and read it in order to save it in the database.

A clear decision would be greatly appreciated. Thanks.

+7
source share
3 answers

What you can do is save the file on the server, after you read its contents, you can delete the file.

I think you cannot read from the client side. You must upload it to ur server in order to read this.

using (StreamReader CsvReader = new StreamReader(input_file)) { string inputLine = ""; while ((inputLine = CsvReader.ReadLine()) != null) { values.Add(inputLine.Trim().Replace(",", "").Replace(" ", "")); } CsvReader.Close(); return values; } 
+4
source

You can access data without saving - using the InputStream property

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

and this (see Paulius Zaliaduonis answer)

+2
source
  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"; } 
0
source

All Articles