so I am new to Blazor and kinda have a little experience in C#. I am trying to upload an image to my database and can't seem to figure out what I am doing wrong. Here is my "BlogCreate" file where I can create a blog.
<div class="form-group">
<label for="City" class="control-label">Context</label>
<input form="City" class="form-control" @bind="@blog.Context" />
</div>
<InputFile OnChange="@LoadFiles" multiple />
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" @onclick="@CreateBlog" value="Save"/>
<input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel"/>
</div>
</div>
</div>
</form>
@code {
Blog blog = new Blog();
public byte[] ImgUpload { get; set; }
protected async void CreateBlog()
{
await blogService.InsertBlog(blog);
NavigationManager.NavigateTo("blogs");
}
void Cancel()
{
NavigationManager.NavigateTo("blogs");
}
private void LoadFiles(InputFileChangeEventArgs e, BlogService.ImgUpload)
{
selectedFiles = e.GetMultipleFiles();
message = $"{selectedFiles.Count} file(s) selected";
this.StateHasChanged();
}
}
I tried copying some stuff from Microsofts docs, but I honestly couldn't figure out how to use it. So here is my services code basically functions for getting the blogs and deleting:
//Get blogs
public async Task<List<Blog>> GetAllBlogs()
{
return await _Context.Blogs.ToListAsync();
}
//Create blogs
public async Task<bool> InsertBlog(Blog blog)
{
await _Context.Blogs.AddAsync(blog);
await _Context.SaveChangesAsync();
return true;
}
and lastly my tables in my database:
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Author { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Context { get; set; }
public string Imgname { get; set; }
public byte[] Img { get; set; }
can someone tell me what I'm doing wrong I am struggling to figure out what direction to turn to upload an image to my database. I am using entity framework as well.