Beispiel #1
0
        public async Task <ActionResult> ImportFromFile(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    TempData["Info"] = "Please select a file to upload";
                }
                else if (file.ContentLength > 0)
                {
                    string[] allowedFileExtensions = new string[] { ".csf" };

                    if (!allowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                    {
                        TempData["Info"] = "Please select a file of type: " + string.Join(", ", allowedFileExtensions);
                    }
                    else
                    {
                        StreamReader stream      = new StreamReader(file.InputStream);
                        var          fileContent = stream.ReadToEnd();
                        stream.Close();
                        Dictionary <string, string> fileSettings = JsonSerializerExtensions.FromJson <Dictionary <string, string> >(fileContent);
                        if (fileSettings.Any())
                        {
                            SaveConfigurationChanges(fileSettings);
                            SetAdminSettings(fileSettings);
                        }

                        TempData["Info"] = "Settings uploaded from file and updated";

                        // Wait for settings to be updated before reloading the page
                        await Task.Delay(2000);
                    }
                }
            }

            return(RedirectToAction("Index"));
        }