Home > OS >  Displaying image using base64 string not showing up Image
Displaying image using base64 string not showing up Image

Time:11-08

I'm converting a Memory steam to a byte array and encoding it ToBase64String

   return Ok(Convert.ToBase64String(memoryStream.ToArray()));

On the client side i'm doing the following to display the image

    var response = await client.GetAsync(Navigator.BaseUri  "upload/process");
    if (response.IsSuccessStatusCode)
    {
    imagesrc = string.Format("data:image/jpeg;base64,{0}", response.Content.ToString());
    StateHasChanged();
    }

But the image is not getting displayed, it gives the following error

Failed to load resource: net::ERR_INVALID_URL

CodePudding user response:

You can use ReadAsStringAsync() which serializes the HTTP content to a string as an asynchronous operation.

var response = await client.GetAsync(Navigator.BaseUri  "upload/process");
if (response.IsSuccessStatusCode)
{
    var base64 = await response.Content.ReadAsStringAsync();
    imagesrc = string.Format("data:image/jpeg;base64,{0}", base64);
    StateHasChanged();
}

CodePudding user response:

net::ERR_INVALID_URL means that your base64 (from response.Content.ToString()) is not valid base64.

So the error is in the code you left out. Post the full Controller and Client code. Include parameters and [Attributes].

  • Related