Beispiel #1
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            try
            {
                List <string[]> csvRows = new List <string[]>();
                if (file != null && file.ContentLength > 0)
                {
                    // extract only the filename
                    var fileName = Path.GetFileName(file.FileName);
                    // store the file inside ~/App_Data/uploads folder
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), Guid.NewGuid().ToString() + "_" + fileName);
                    file.SaveAs(path);
                    using (var reader = new StreamReader(System.IO.File.OpenRead(path)))
                    {
                        var parser             = new NetCSVParser();
                        var standartCollLength = reader.ReadLine().Split(',').Length;
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadToEnd();
                            line.Replace(",#N/A,", ",null,");
                            List <string[]> values = new List <string[]>();
                            values = parser.Parse(path, standartCollLength, ",");
                            values.RemoveAt(0);
                            csvRows = values;
                        }
                    }
                }

                if (csvRows.Count > 0)
                {
                    int expectedLength = csvRows[0].Length;
                    foreach (var item in csvRows)
                    {
                        if (item.Length != expectedLength)
                        {
                            throw new ArgumentException("CSV file is invalid");
                        }
                    }
                }

                if (csvRows.Count() < 1)
                {
                    throw new ArgumentException("File is empty or invalid");
                }

                Dictionary <String, CPL> cplDictionary = _dbContext.CPLs.ToDictionary(x => x.PropertyCode);

                int skippedRows = 0;
                for (int i = 0; i < csvRows.Count; i++)
                {
                    InquiriesValidation str = csvRows[i].ToInquiriesValidation();

                    if (!cplDictionary.ContainsKey(str.PropertyCode))
                    {
                        skippedRows++;
                        continue;
                    }

                    str.CPL = cplDictionary[str.PropertyCode];
                    _dbContext.InquiriesValidations.Add(str);
                }
                saveShowErrors(_dbContext);
                ViewBag.UploadMessage = "Data is succesfully uploaded." + (csvRows.Count - skippedRows) + " rows added, " + skippedRows + " rows skipped (no property code found)";
                return(View());
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = "Error while uploading data: " + ex.GetType().Name + ".";

                string message = ViewBag.ErrorMessage + ex.Message;
                RDTLogger.Info(message, typeof(InquiriesValidationController));
                return(View());
            }
        }
Beispiel #2
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            try
            {
                List <string[]> csvRows = new List <string[]>();
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path     = Path.Combine(Server.MapPath("~/App_Data/uploads"), Guid.NewGuid().ToString() + "_" + fileName);
                    file.SaveAs(path);

                    using (var reader = new StreamReader(System.IO.File.OpenRead(path)))
                    {
                        var colCount = reader.ReadLine().Split(',').Length;
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadToEnd();
                            line.Replace(",#N/A,", ",null,");
                            List <string[]> values = new List <string[]>();
                            var             parser = new NetCSVParser();
                            values = parser.Parse(path, colCount, "\t");
                            values.RemoveAt(0);

                            csvRows = values;
                        }
                    }
                }

                if (csvRows.Count > 0)
                {
                    int expectedLength = csvRows[0].Length;
                    foreach (var item in csvRows)
                    {
                        if (item.Length != expectedLength)
                        {
                            throw new ArgumentException("TSV file is invalid");
                        }
                    }
                }

                if (csvRows.Count() < 1)
                {
                    throw new ArgumentException("File is empty or invalid");
                }

                for (int i = 0; i < csvRows.Count; i++)
                {
                    AirbnbAccount str = csvRows[i].ToAirbnbAccounts();
                    _dbContext.AirbnbAccounts.Add(str);
                }
                saveShowErrors(_dbContext);
                _dbContext.SaveChanges();
                ViewBag.UploadMessage = "Data is succesfully uploaded";
                return(View());
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = "Error while uploading data: " + ex.GetType().Name + ". ";

                string message = ViewBag.ErrorMessage + ex.Message;
                RDTLogger.Info(message, typeof(InquiriesValidationController));
                return(View());
            }
        }