otkalce

MVC Form POST handler (+file)

Apr 20th, 2023
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | Source Code | 0 0
  1. private static byte[] GetFileByteAray(IFormFile formFile)
  2. {
  3.     if (formFile != null)
  4.     {
  5.         if (formFile.Length > 0)
  6.         {
  7.             using (var memoryStream = new MemoryStream())
  8.             {
  9.                 formFile.CopyTo(memoryStream);
  10.  
  11.                 if (memoryStream.Length < 50 * 1024 * 1024)
  12.                 {
  13.                     return memoryStream.ToArray();
  14.                 }
  15.             }
  16.  
  17.         }
  18.     }
  19.  
  20.     return null;
  21. }
  22.  
  23. [HttpPost]
  24. public IActionResult Edit(VMFormData vmFormData)
  25. {
  26.     var dbFormData = _dbContext.FormData.FirstOrDefault(x => x.Id == vmFormData.Id);
  27.     dbFormData.TestText = vmFormData.TestText;
  28.     dbFormData.TestTel = vmFormData.TestTel;
  29.     dbFormData.TestUrl = vmFormData.TestUrl;
  30.     dbFormData.TestEmail = vmFormData.TestEmail;
  31.     dbFormData.TestNumber = vmFormData.TestNumber;
  32.     dbFormData.TestPassword = vmFormData.TestPassword ?? dbFormData.TestPassword;
  33.     dbFormData.TestCheckbox1 = vmFormData.TestCheckbox1;
  34.     dbFormData.TestCheckbox2 = vmFormData.TestCheckbox2;
  35.     dbFormData.TestRadio = vmFormData.TestRadio;
  36.     dbFormData.TestColor = vmFormData.TestColor;
  37.     dbFormData.TestRange = vmFormData.TestRange;
  38.     dbFormData.TestDate1 = vmFormData.TestDate1;
  39.     dbFormData.TestDate2 = vmFormData.TestDate2;
  40.     dbFormData.TestDateTime = vmFormData.TestDateTime;
  41.     dbFormData.TestMonth = vmFormData.TestMonth;
  42.     dbFormData.TestTime = vmFormData.TestTime;
  43.     dbFormData.TestWeek = vmFormData.TestWeek;
  44.     dbFormData.TestHidden = vmFormData.TestHidden;
  45.  
  46.     // BEGIN: Handle file in database
  47.     var fileArray = GetFileByteAray(vmFormData.TestFile);
  48.     if (fileArray != null)
  49.     {
  50.         var dbFormDataFile = _dbContext.FormDataFiles.FirstOrDefault(x => x.Id == vmFormData.Id);
  51.         if (dbFormDataFile == null)
  52.         {
  53.             _dbContext.FormDataFiles.Add(new FormDataFile()
  54.             {
  55.                 IdNavigation = dbFormData,
  56.                 FileContent = Convert.ToBase64String(fileArray),
  57.                 Name = vmFormData.TestFile.FileName
  58.             });
  59.         }
  60.         else
  61.         {
  62.             dbFormDataFile.FileContent = Convert.ToBase64String(fileArray);
  63.             dbFormDataFile.Name = vmFormData.TestFile.FileName;
  64.         }
  65.     }
  66.     // END: Handle file in database
  67.  
  68.     _dbContext.SaveChanges();
  69.  
  70.     return RedirectToAction(nameof(Index));
  71. }
Tags: dotnet-mvc
Add Comment
Please, Sign In to add comment