Monday 13 June 2016

How to read text file in MVC

using System.Transactions;
using System.IO;
 
 public ActionResult Save(B2CFileUpload b2CFileUpload)
        {
            if (b2CFileUpload.Upload.ContentType.Trim() == "text/plain".Trim())
            {
                try
                {
                    using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(300)))
                    {

                   
                        List<SalesOrderItem> lstSalesOrderItem = new List<SalesOrderItem>();
                        DirectoryInfo ActualFolder = new DirectoryInfo(Server.MapPath("~/Administration/Content/B2CFiles/"));
                        DirectoryInfo NewFolder = ActualFolder.CreateSubdirectory(DateTime.UtcNow.ToShortDateString().Replace('/', '-'));
                        b2CFileUpload.Upload.SaveAs(NewFolder.FullName + "/" + Path.GetFileName(b2CFileUpload.Upload.FileName));
                        StreamReader streamReader = new StreamReader(System.IO.File.OpenRead(NewFolder.FullName + "/" + Path.GetFileName(b2CFileUpload.Upload.FileName)));

                        while (!streamReader.EndOfStream)
                        {
                            string row = streamReader.ReadLine();
                            if (i == 0)
                            {
                                string[] Columns = row.Split('-');
                                string[] SalesTaxvalues = Columns[1].Split('.');
                                SalesTax = SalesTaxvalues[1].Substring(2, 6) + "." + SalesTaxvalues[2].Substring(0, 2);
                            }
                            else
                            {
                                string[] Columns = row.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                CustId = Columns[0];
                                ItemId = Columns[1].Substring(13);
                                string[] check = Columns[2].Split('.');
                                ItemPrice = check[0] + "." + check[1].Substring(0, 2);
                                //ItemQuantity = check[1].Substring(2, 6) + "." + check[2].Substring(0, 2);
                                ItemQuantity = check[1].Substring(2, 6);
                                LineTotal = check[2].Substring(2, 6) + "." + check[3].Substring(0, 3);
                             
                            }

                            i++;
                        }
                     
                        }
                        Invoice invoice = _invoiceService.InsertInvoice(invoiceModel.ToEntity());
                        TempData["Update"] = (i - 1);
                        TempData.Keep("Update");
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    TempData["Error"] = ex.Message;
                    TempData.Keep("Error");
                }




            }
            return RedirectToAction("Index");
        }

No comments:

Post a Comment