0

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

Update: This how the image is set

<img src="@imagesrc" width="300px" height="300px" style="border: 1px solid #4f4f50; border-radius: 1px" />
1
  • You don't show what you do with imagesrc Commented Nov 8, 2022 at 11:29

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

@HenkHolterman Yes peaking the base64 string with debugger will be enlightening indeed.
@IbrahimTimimi I have tried response.Content.ReadAsStringAsync() but it wont work either, the same issue
It has started to work after i replaced the client side async code imagesrc = string.Format("data:image/jpeg;base64,{0}", response.Content.ReadAsStringAsync().Result); Seems it was getting some thread input , not the data
@techno I assumed if you use await then you will get the content instead of the task. Did you try await response.Content.ReadAsStringAsync()?
2

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].

2 Comments

The controller is doing a lot of stuff from backend.Like pulling data from azure blob container,that is why stripped that code. I can provide you with the content of the base 64 string in the client side justpaste.it/715jt
It has started to work after i replaced the client side async code imagesrc = string.Format("data:image/jpeg;base64,{0}", response.Content.ReadAsStringAsync().Result); Seems it was getting some thread input , not the data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.