WinRT StorageFile write downloaded file

后端 未结 3 697
不思量自难忘°
不思量自难忘° 2021-01-04 22:23

I\'m struggling with a easy problem. I want to download an image from web using this code:

WebRequest requestPic = WebRequest.Create(@\"http://something.com/         


        
3条回答
  •  遥遥无期
    2021-01-04 23:06

    You will need to read the response stream into a buffer then write the data to a StorageFile. THe following code shows an example:

            var fStream = responsePic.GetResponseStream();
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("testfile.txt");
            using (var ostream = await file.OpenStreamForWriteAsync())
            {
                int count = 0;
                do
                {
                    var buffer = new byte[1024];
                    count = fStream.Read(buffer, 0, 1024);
                    await ostream.WriteAsync(buffer, 0, count);
                }
                while (fStream.CanRead && count > 0);
            }
    

提交回复
热议问题