Skip to content

Commit

Permalink
Issue #146 image saved even if the action is aborted (#152)
Browse files Browse the repository at this point in the history
* fix #146

* fix #146
  • Loading branch information
Sben65 authored Jan 31, 2022
1 parent a432137 commit 8c17e66
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,21 @@
ModelId = Guid.NewGuid().ToString()
};

private MultipartFormDataContent content = null;

private DeviceModelCommand DeviceModelCommand { get; set; } = new DeviceModelCommand();
private string imageDataUrl;

async Task DeleteAvatar()
private void DeleteAvatar()
{
await httpClient.DeleteAsync($"api/DeviceModels/{DeviceModel.ModelId}/avatar");
// await httpClient.DeleteAsync($"api/DeviceModels/{DeviceModel.ModelId}/avatar");
content = null;
imageDataUrl = null;
}

private async Task UploadFiles(InputFileChangeEventArgs e)
{
using var content = new MultipartFormDataContent();
content = new MultipartFormDataContent();

var resizedImageFile = await e.File.RequestImageFileAsync(e.File.ContentType, 200, 200);

Expand All @@ -142,15 +145,23 @@
name: "\"file\"",
fileName: e.File.Name);

var response = await httpClient.PostAsync($"api/DeviceModels/{DeviceModel.ModelId}/avatar", content);
// var response = await httpClient.PostAsync($"api/DeviceModels/{DeviceModel.ModelId}/avatar", content);
// imageDataUrl = await response.Content.ReadAsStringAsync();
var buffer = new byte[resizedImageFile.Size];
await resizedImageFile.OpenReadStream().ReadAsync(buffer);

imageDataUrl = await response.Content.ReadAsStringAsync();
imageDataUrl = $"data:{e.File.ContentType};base64,{Convert.ToBase64String(buffer)}";
}

private async Task Save()
{
var result = await httpClient.PostAsJsonAsync("api/DeviceModels", DeviceModel);

if (content is not null)
{
var response = await httpClient.PostAsync($"api/DeviceModels/{DeviceModel.ModelId}/avatar", content);
}

if (result.IsSuccessStatusCode)
{
DeviceModel = new DeviceModel();
Expand Down

0 comments on commit 8c17e66

Please sign in to comment.