Download an image to local storage in Metro style apps

前端 未结 5 1250
一向
一向 2020-12-16 07:22

In WinRT / C#, How do I download an image to a local folder to support caching of an online catalogue for offline use? is there a way to directly download the images and lin

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 07:33

    Try this:

            var response = await HttpWebRequest.Create(url).GetResponseAsync();
            List allBytes = new List();
            using (Stream imageStream = response.GetResponseStream())
            {
                byte[] buffer = new byte[4000];
                int bytesRead = 0;
                while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0)
                {
                    allBytes.AddRange(buffer.Take(bytesRead));
                }
            }
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                        "localfile.png", CreationCollisionOption.FailIfExists);
            await FileIO.WriteBytesAsync(file, allBytes.ToArray()); 
    

提交回复
热议问题