Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the Uploaded CSV file without saving it to Server?

Tags:

c#

csv

Hi all, I have CSV files which are in this format:

**CSV Format1**

  ||OrderGUID||OrderItemID||Qty||SKUID||TrackingNumber||TotalWeight||DateShipped||DateDelivered||ShippingStatusId||OrderShippingAddressId
  ||5        ||3          ||2  ||12312||aasdasd       ||24         ||2012-12-2010||            || 10025          ||10028
  ||5        ||4          ||3  ||113123||adadasdasd   ||22         ||2012-12-2012||            ||10026           ||10028



**CSV Format2**

    ||"OrderGUID"||"OrderItemID"||"Qty"||"SKUID"||"TrackingNumber"||"TotalWeight"||"DateShipped"||"DateDelivered"||"ShippingStatusId"||"OrderShippingAddressId"||
    ||"5"        ||"3"          ||"2"  ||"12312"||"aasdasd"       ||"24"         ||"2012-12-2010"||""            || "10025"          ||"10028"||
    ||"5"        ||"4"          ||"3"  ||"113123"||"adadasdasd"   ||"22"         ||"2012-12-2012"|| "2012-12-2010" ||"10026"           ||"10028"||

I have to read these files without saving them on the server. Can anyone help me? How can I read this files and insert in my db? How can I trim the special characters from the files?

This is what I am trying to do for the file upload:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ImportTrackingNumber(FormCollection form,HttpPostedFileBase UploadedFile,TrackingNumbersModel Trackingnumbers)
{
    if (UploadedFile != null)
    {
        var allowedExtensions = new[] {".xlsx", ".csv"};
        if (UploadedFile.ContentLength > 0)
        {                   
            var extension = Path.GetExtension(UploadedFile.FileName);
            if (extension == ".xlsx")
            {
                //Need To code For Excel Files Reading
            }
            else if (extension == ".csv")
            {
                //string filename = Path.GetFileName(UploadedFile.PostedFile.InputStream);
                StreamReader csvreader = new StreamReader(UploadedFile.FileName);
                DataTable dt;        
            }
        }
    }
    return View();
}
like image 997
SoftwareNerd Avatar asked Dec 13 '12 15:12

SoftwareNerd


People also ask

How do I view a local CSV file?

open the file using open(). The open() takes two parameters, the name of the file and the mode in which you want to open it. Here the mode is 'r' since we need to read the file. Read the contents of the file using csv.

How is CSV data stored?

A CSV file typically stores tabular data (numbers and text) in plain text, in which case each line will have the same number of fields. The CSV file format is not fully standardized. Separating fields with commas is the foundation, but commas in the data or embedded line breaks have to be handled specially.


1 Answers

Just an example on how you can read the uploaded file without saving it on the server:

// Use the InputStream to get the actual stream sent.
using (StreamReader csvReader = new StreamReader(UploadedFile.InputStream)) 
{
    while (!csvReader.EndOfStream)
    {
        var line = csvReader.ReadLine();
        var values = line.Split(';');
    }
}
like image 76
Mario S Avatar answered Oct 13 '22 01:10

Mario S