本文介绍了WinRT StorageFile 写入下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为一个简单的问题而苦苦挣扎.我想使用以下代码从网络下载图像:
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/" + id + ".jpg");
WebResponse responsePic = await requestPic.GetResponseAsync();
现在我想在 StorageFile 中写入 WebResponse 的流(例如,在应用程序的存储中创建一个文件 id.jpg),但我还没有找到任何方法来实现这一点.我在网上搜索它,但没有成功 - 所有方式不兼容的 Stream 类型等等.
Now I wanted to write the WebResponse's stream in a StorageFile (eg. create a file id.jpg in the app's storage), but I haven't found any way to achieve that. I searched the web for it, but no success - all ways incompatible Stream types and so on.
你能帮忙吗?
推荐答案
我找到了以下解决方案,它有效且不太复杂.
I have found the following solution, which works and is not too complicated.
public async static Task<StorageFile> SaveAsync(
Uri fileUri,
StorageFolder folder,
string fileName)
{
var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
var downloader = new BackgroundDownloader();
var download = downloader.CreateDownload(
fileUri,
file);
var res = await download.StartAsync();
return file;
}
这篇关于WinRT StorageFile 写入下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!