Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static byte[] GetFileByteAray(IFormFile formFile)
- {
- if (formFile != null)
- {
- if (formFile.Length > 0)
- {
- using (var memoryStream = new MemoryStream())
- {
- formFile.CopyTo(memoryStream);
- if (memoryStream.Length < 50 * 1024 * 1024)
- {
- return memoryStream.ToArray();
- }
- }
- }
- }
- return null;
- }
- [HttpPost]
- public IActionResult Edit(VMFormData vmFormData)
- {
- var dbFormData = _dbContext.FormData.FirstOrDefault(x => x.Id == vmFormData.Id);
- dbFormData.TestText = vmFormData.TestText;
- dbFormData.TestTel = vmFormData.TestTel;
- dbFormData.TestUrl = vmFormData.TestUrl;
- dbFormData.TestEmail = vmFormData.TestEmail;
- dbFormData.TestNumber = vmFormData.TestNumber;
- dbFormData.TestPassword = vmFormData.TestPassword ?? dbFormData.TestPassword;
- dbFormData.TestCheckbox1 = vmFormData.TestCheckbox1;
- dbFormData.TestCheckbox2 = vmFormData.TestCheckbox2;
- dbFormData.TestRadio = vmFormData.TestRadio;
- dbFormData.TestColor = vmFormData.TestColor;
- dbFormData.TestRange = vmFormData.TestRange;
- dbFormData.TestDate1 = vmFormData.TestDate1;
- dbFormData.TestDate2 = vmFormData.TestDate2;
- dbFormData.TestDateTime = vmFormData.TestDateTime;
- dbFormData.TestMonth = vmFormData.TestMonth;
- dbFormData.TestTime = vmFormData.TestTime;
- dbFormData.TestWeek = vmFormData.TestWeek;
- dbFormData.TestHidden = vmFormData.TestHidden;
- // BEGIN: Handle file in database
- var fileArray = GetFileByteAray(vmFormData.TestFile);
- if (fileArray != null)
- {
- var dbFormDataFile = _dbContext.FormDataFiles.FirstOrDefault(x => x.Id == vmFormData.Id);
- if (dbFormDataFile == null)
- {
- _dbContext.FormDataFiles.Add(new FormDataFile()
- {
- IdNavigation = dbFormData,
- FileContent = Convert.ToBase64String(fileArray),
- Name = vmFormData.TestFile.FileName
- });
- }
- else
- {
- dbFormDataFile.FileContent = Convert.ToBase64String(fileArray);
- dbFormDataFile.Name = vmFormData.TestFile.FileName;
- }
- }
- // END: Handle file in database
- _dbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
Add Comment
Please, Sign In to add comment