Advertisement
Dimitar46

Import method

Apr 10th, 2024
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1.  [HttpPost]
  2.  public async Task<IActionResult> Import(IFormFile file)
  3.  {
  4.      if (file != null && file.Length > 0)
  5.      {
  6.          Console.WriteLine($"File name: {file.FileName}");
  7.          Console.WriteLine($"File size: {file.Length} bytes");
  8.          if (file.ContentType == "application/json" || file.ContentType == "text/json")
  9.          {
  10.  
  11.              using (var reader = new StreamReader(file.OpenReadStream()))
  12.              {
  13.                  var jsonContent = await reader.ReadToEndAsync();
  14.  
  15.                  string schemaFilePath = Path.Combine(Directory.GetCurrentDirectory(), "schema.json");
  16.  
  17.  
  18.                  var schemaJson = System.IO.File.ReadAllText(schemaFilePath);
  19.                  JSchema schema = JSchema.Parse(schemaJson);
  20.  
  21.              
  22.                  JObject jsonDoc = JObject.Parse(jsonContent);
  23.  
  24.                  
  25.                  bool isValid = jsonDoc.IsValid(schema);
  26.  
  27.                  if (isValid)
  28.                  {
  29.                  
  30.  
  31.                  
  32.                      List<Project> projects;
  33.                      try
  34.                      {
  35.                          JArray projectsArray = (JArray)jsonDoc["projects"];
  36.                          projects = projectsArray.ToObject<List<Project>>();
  37.                      }
  38.  
  39.                      catch (JsonException ex)
  40.                      {
  41.                          ModelState.AddModelError("", "Error deserializing JSON data: " + ex.Message);
  42.                          return View();
  43.                      }
  44.  
  45.                    
  46.                      try
  47.                      {
  48.                          foreach (var project in projects)
  49.                          {
  50.                              _context.Projects.Add(project);
  51.                          }
  52.                          await _context.SaveChangesAsync();
  53.                      }
  54.                      catch (DbUpdateException ex)
  55.                      {
  56.                          ModelState.AddModelError("", "Error saving projects to the database: " + ex.Message);
  57.                          return View();
  58.                      }
  59.  
  60.                    
  61.                      TempData["ImportSuccessMessage"] = $"{projects.Count} projects were successfully imported.";
  62.  
  63.                  
  64.                      return RedirectToAction("Index","Home");
  65.                  }
  66.                  else
  67.                  {
  68.                      
  69.                      ModelState.AddModelError("", "JSON data is invalid.");
  70.                      return View();
  71.                  }
  72.              }
  73.          }
  74.          else
  75.          {
  76.              ModelState.AddModelError("", "Unsupported file format. Please upload a JSON file.");
  77.              return View();
  78.          }
  79.  
  80.      
  81.      }
  82.      else
  83.      {
  84.          ModelState.AddModelError("", "Please select a file to upload.");
  85.          return View();
  86.      }
  87.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement