简体   繁体   English

无法从 system.threding.Tasks.task 转换<byte[]>到字节[]</byte[]>

[英]cannot convert from system.threding.Tasks .task<byte[]> to byte[]

I am new to c# and react.我是 c# 的新手并做出反应。 I am using the following method to convert image url to bytes我正在使用以下方法将图像 url 转换为字节

return Convert.ToBase64String(bytes);

but I am getting an error which says但我收到一个错误,上面写着

cannot convert from System.Threading.Tasks.Task<byte[]> to byte[]无法从System.Threading.Tasks.Task<byte[]>转换为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?我该如何纠正这个错误?

GetByteArrayAsync is an async method, which returns a Task. GetByteArrayAsync是一个异步方法,它返回一个任务。 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);
    }
}

Since GetByteArrayAsync returns Task, you must wait for the task to complete:由于 GetByteArrayAsync 返回 Task,您必须等待任务完成:

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第二种方式通常比第一种更好,它不会阻塞线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM