コード例 #1
0
        public async Task <ActionResult> Index(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return(View("Index", "Файл пуст или не выбран"));
            }

            var excel             = new ExcelWorker();
            var azureStorageTable = new AzureCloudStorageWorker();

            try
            {
                await excel.Load(file);

                if (!excel.CheckRequiredColumns(new List <string> {
                    "фио", "телефон"
                }))
                {
                    return(View("Index", "Ошибка - отсутствуют обязательные для заполнения колонки"));
                }

                var connectionString = Program.Configuration["ConnectionStrings:AzureTableStorage"];
                if (!await azureStorageTable.ConnectAsync(connectionString, "Contacts"))
                {
                    return(View("Index", "Ошибка соединения с хранилищем"));
                }

                await azureStorageTable.InsertOrReplace(excel.Read(true));

                return(View("Index", $"Записано {excel.Saved} контактов, пропущено - {excel.Skipped}"));
            }
            catch (Exception ex)
            {
                return(View("Index", $"Ошибка - {ex.Message}"));
            }
        }