Пример #1
0
        public IActionResult Import()
        {
            ViewData["importeddata"] = 0;
            ViewData["hasimport"]    = false;
            SMSImportModel model = new SMSImportModel();

            return(View(model));;
        }
Пример #2
0
        public async Task <IActionResult> Import(SMSImportModel model, string processType, int smsStatus)
        {
            model.FormBehavior = new FormBehavior();
            model.FormBehavior.Notification = null;
            int importcount = 0;

            ViewData["importeddata"] = importcount;
            ViewData["hasimport"]    = false;

            string folderName  = "Upload";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string uploadPath  = Path.Combine(webRootPath, folderName);

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            if (processType == "CommitImport")
            {
                if (!string.IsNullOrWhiteSpace(model.FileName))
                {
                    try
                    {
                        var records = ExcelToSMSList(uploadPath, model.FileName, true);
                        foreach (var item in records)
                        {
                            try
                            {
                                _smsService.NewSMS(
                                    new ShortMessageService
                                {
                                    SMSStatus    = (Enums.SMSStatus)smsStatus,
                                    MobileNumber = item.MobileNumber,
                                    MessageBody  = item.MessageBody
                                }, true
                                    );
                                importcount += 1;
                            }
                            catch {}
                        }
                        model.FormBehavior.Notification = new Notification
                        {
                            IsError = false,
                            Message = "Records successfully imported.",
                            Title   = "Short Message Service"
                        };
                        ViewData["importeddata"] = importcount;
                        model.Records            = new List <SMSImportList>();
                    }
                    catch (Exception ex)
                    {
                        model.FormBehavior.Notification = new Notification
                        {
                            IsError = false,
                            Message = ex.Message,
                            Title   = "Short Message Service"
                        };
                    }
                    finally
                    {
                        model.FileName = string.Empty;
                    }
                }
            }
            else if (processType == "ProcessImport")
            {
                model.Records = new List <SMSImportList>();
                var file = model.FileAttachment;

                if (file != null && file.Length > 0)
                {
                    string fullPath = Path.Combine(uploadPath, file.FileName);
                    using (var fileStream = new FileStream(fullPath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    try
                    {
                        model.FileName = file.FileName;
                        model.Records  = ExcelToSMSList(uploadPath, file.FileName);
                        ModelState.Clear();
                    }
                    catch (Exception ex)
                    {
                        model.FormBehavior.Notification = new Notification
                        {
                            IsError = true,
                            Message = ex.Message,
                            Title   = "Short Message Service"
                        };
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Please attach a file.");
                }
            }
            return(PartialView("_Import", model));
        }