Working with System.Threading.Tasks.Task instead of Stream

前端 未结 3 819
挽巷
挽巷 2021-02-15 15:27

I was using a method like below on the previous versions of WCF Web API:

// grab the posted stream
Stream stream = request.Content.ContentReadStream;

// write i         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 15:51

    Here is how you can do this better with async and await:

        private async void WhatEverMethod()
        {
            var stream = await response.Content.ReadAsStreamAsync();
    
            using (FileStream fileStream = File.Create(fullFileName, (int)stream.Length))
            {
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
            }
        });
    

提交回复
热议问题