Exemplo n.º 1
0
        public ActionResult ImportCategories(HttpPostedFileBase postedExcelFile)
        {
            if (ModelState.IsValid)
            {
                if (postedExcelFile == null)
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                    TempData["Error"] = "Please Upload Your file.";
                    return(RedirectToAction("Index"));
                }
                else if (postedExcelFile.ContentLength > UtilityConstants.MaxContentLength)
                {
                    TempData["Error"] = "SizeExceed";
                    return(RedirectToAction("Index"));
                    //return Content("SizeExceed");
                }
                else if (postedExcelFile.ContentLength > 0)
                {
                    string path = ExcelHelper.SavePathForThePostedFile(postedExcelFile);

                    if (!(path.Contains("xlsx") || path.Contains("xls")))
                    {
                        return(Content("FileFormatError"));
                    }

                    try
                    {
                        List <CategoryImportExcel> records = ExcelHelper.ReadSheet <CategoryImportExcel>(path, true, 0, null, true).ToList();
                        records = records.Where(r => !string.IsNullOrEmpty(r.CategoryName) && !string.IsNullOrEmpty(r.CreatedByUser)).ToList();
                        if (records.Count > 0)
                        {
                            objCategoryBO.InsertCategoryInBulk(records);
                            TempData["Success"] = $"\"NumberOfRecords Uploaded\" : {records.Count()}";
                            return(RedirectToAction("Index"));
                        }
                        TempData["Success"] = $"\"NumberOfRecords Uploaded\" : 0";
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("InValidZipCode"))
                        {
                            return(Content(ex.Message));
                        }
                    }
                }
            }
            return(Content("Error"));
            //if (Request.Files["postedExcelFile"].ContentLength > 0)
            //{
            //    string fileExtension = System.IO.Path.GetExtension(Request.Files["postedExcelFile"].FileName);
            //}
            // return View();
        }