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();