本文介绍了WinRT StorageFile写下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力解决一个容易的问题。我想使用以下代码从网站下载图片: WebRequest requestPic = WebRequest.Create(@http:// something .com /+ id +.jpg);
WebResponse responsePic = await requestPic.GetResponseAsync();
现在我想将WebResponse的流写入一个StorageFile(例如,创建一个文件id.jpg应用程序的存储),但是我没有找到任何方法来实现。我搜索了网页,但没有成功 - 所有方式都不兼容流类型等。
请问你可以帮忙吗?
解决方案
我已经找到了以下解决方案,它的工作并不复杂。
public async static任务< StorageFile> SaveAsync(
Uri fileUri,
StorageFolder文件夹,
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();
返回文件;
}
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();
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.
Could you please help?
解决方案
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写下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!