From 8c17e669ab28dc737fa391ac91a6d6269ccc10a5 Mon Sep 17 00:00:00 2001 From: salim ben ahben <40862545+Sben65@users.noreply.github.com> Date: Mon, 31 Jan 2022 17:03:26 +0100 Subject: [PATCH] Issue #146 image saved even if the action is aborted (#152) * fix #146 * fix #146 --- .../DeviceModels/CreateDeviceModelPage.razor | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor index 4d3d655b1..dcfcc9bc4 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/DeviceModels/CreateDeviceModelPage.razor @@ -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); @@ -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();