Exemplo n.º 1
0
        public ActionResult NewUserMTDDataSheet(int month, int year, HttpPostedFileBase file)
        {
            var viewModel = new NewUserDataSheetViewModel
            {
                Year   = year,
                Month  = month,
                Years  = MonthYearHelper.GetYearList(),
                Months = MonthYearHelper.GetMonthList()
            };

            if (file != null && file.ContentLength > 0)
            {
                if (file.FileName.EndsWith(".csv"))
                {
                    using (var stream = file.InputStream)
                    {
                        using (var csvTable = new DataTable())
                        {
                            using (var reader = new CsvReader(new StreamReader(stream), true))
                            {
                                csvTable.Load(reader);
                                var headers = new List <string> {
                                    "TM CODE", "BRAND", "Date", "FIRST USE"
                                };
                                if (!csvTable.IsHeaderValid(headers))
                                {
                                    ModelState.AddModelError("", String.Format("Uploaded file does not contain all the required headers: {0}", String.Join(",", headers)));
                                    return(View(viewModel));
                                }
                                _dataSheetService.UploadNewUserMTDDataSheet(year, csvTable);
                            }
                        }
                    }
                }
            }

            //TempData["Message"] = "New User MTD Datasheet has been successfully uploaded";
            return(RedirectToAction("NewUserMTDDataSheet", new { month, year }));
        }