Hi @Robert Brown,
When I use a byte array type for the property/method in the entity model class, nothing is saved to the database. I have tried using an IFormFile type for the property/method in my entity model class, but it does not convert to the byte array type needed to save the file to the database. I have also used the 'image' SQL datatype for saving an image, and the validation causes an invalid image error.
You could refer the following sample:
1.You could create the data class for EF to store the file to the database.
public class Applicationfile
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public byte[] FileContent { get; set; }
public string Description { get; set; }
}
2.Add the dbset to the dbcontext
public DbSet<Applicationfile> Applicationfiles { get; set; }
3.Define a filemodel for uploading the file and other property inside the razor page
public class FileViewModel
{
public IFormFile FormFile { get; set; }
public string description { get; set; }
}
4.Modify the razor page like below:
Cs file:
public class CreateFileModel : PageModel
{
private readonly ApplicationDbContext _ApplicationDbContext ;
public CreateFileModel(ApplicationDbContext ApplicationDbContext) {
_ApplicationDbContext = ApplicationDbContext;
}
public void OnGet()
{
}
[BindProperty]
public FileViewModel FileUpload { get; set; }
public async Task<IActionResult> OnPostUploadFileAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
string fileName = Path.GetFileName(FileUpload.FormFile.FileName);
string contentType = FileUpload.FormFile.ContentType;
using (MemoryStream memoryStream = new MemoryStream())
{
FileUpload.FormFile.CopyTo(memoryStream);
var file = new Applicationfile()
{
Name = FileUpload.FormFile.FileName,
FileContent = memoryStream.ToArray(),
Description = FileUpload.description
};
_ApplicationDbContext.Applicationfiles.Add(file);
await _ApplicationDbContext.SaveChangesAsync();
}
return RedirectToPage("Index");
}
}
Razor page:
<div class="row">
<div class="col-md-4">
<form method="post" enctype="multipart/form-data" >
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FileUpload.FormFile" class="control-label"></label>
<input asp-for="FileUpload.FormFile" class="form-control" type="file" />
<span asp-validation-for="FileUpload.FormFile" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FileUpload.description" class="control-label"></label>
<input asp-for="FileUpload.description" class="form-control" />
<span asp-validation-for="FileUpload.description" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" asp-page-handler="UploadFile" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Result:

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.