0

I am new to c# and react. I am using the following method to convert image url to bytes

return Convert.ToBase64String(bytes);

but I am getting an error which says

cannot convert from System.Threading.Tasks.Task<byte[]> to byte[]

This is the method:

[HttpGet]      
[Route("GetImages")]
public  IHttpActionResult GetImages()
{    
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
        myproxy.BypassProxyOnLocal = false;
    //myproxy.UseDefaultCredentials = true;    

    HttpClientHandler handler = new HttpClientHandler()
    {
        Proxy = myproxy
    };

    using (var client = new HttpClient(handler))
    {
        var bytes = 
             client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil- 
        qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926? 
        alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
           
        return Convert.ToBase64String(bytes);
    }    
}

How do I correct this error?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
sambit
  • 256
  • 2
  • 18

2 Answers2

2

GetByteArrayAsync is an async method, which returns a Task. You need to await the task to get the return value. In order to await it, the action method has to be async.

[HttpGet]
[Route("GetImages")]
public async Task<IHttpActionResult> GetImages()
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
    myproxy.BypassProxyOnLocal = false;
    HttpClientHandler handler = new HttpClientHandler()
    {
        Proxy = myproxy
    };
    using (var client = new HttpClient(handler))
    {
        var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil- 
             qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926? 
             alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
           
        return Convert.ToBase64String(bytes);
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
1

Since GetByteArrayAsync returns Task, you must wait for the task to complete:

var bytes = client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil- 
    qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926? 
    alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4").Result

OR

var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil- 
    qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926? 
    alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4")

The second way is usually better than the first, it does not block the thread

FatTiger
  • 647
  • 5
  • 14
  • 2
    I would not encourage the OP to use `.Result`. If (s)he needs to call the async method synchronously then prefer `.GetAwaiter().GetResult()` [Reference](https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult) – Peter Csala Jun 09 '21 at 07:36