Windows Metro / 8 - 从URL下载图像文件并将其保存到appdata [英] Windows Metro/8 - download image file from URL and save it to appdata
问题描述
我知道webclient已被弃用,所以我一直在尝试使用HTTPclient,但是当我尝试时,并没有保存整个图像,它缺少一些字节(?)
如何从URL下载图像,并将其保存在硬盘驱动器(appdata文件夹)中。这有多难?似乎.net使它变得比它需要的更难。
您可以尝试使用BackgroundDownloader类下载图像并将其存储在AppData文件夹中。有关详细示例代码,您可以查看背景转移示例情景1。
http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61
此外,您可以按照这些代码进行操作。
Uri source = new Uri(" http://www.xxxxxx/xxx.jpg");
StorageFile destinationFile;
try
{
destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
" downloadimage.jpg",CreationCollisionOption.GenerateUniqueName);
}
catch(FileNotFoundException ex)
{
return;
}
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source,destinationFile);
await download.StartAsync();
ResponseInformation response = download.GetResponseInformation();最好的问候,
Jesse
Hi, I know webclient has been deprecated so I've been trying to use HTTPclient but when I try, not the entire image is saved, it is missing some bytes(?)
How can I download an image from a URL, and save it on the hard drive(appdata folder). How hard can this be? It seems like .net is making it harder than it needs to be.
Hi,
You can try the BackgroundDownloader class to download a image and store it in AppData folder. For more detail sample codes, you can check Background Transfer sample scenariol 1.
http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61Also, you can follow these codes.
Uri source= new Uri("http://www.xxxxxx/xxx.jpg"); StorageFile destinationFile; try { destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync( "downloadimage.jpg", CreationCollisionOption.GenerateUniqueName); } catch (FileNotFoundException ex) { return; } BackgroundDownloader downloader = new BackgroundDownloader(); DownloadOperation download = downloader.CreateDownload(source, destinationFile); await download.StartAsync(); ResponseInformation response = download.GetResponseInformation();Best regards,
Jesse
这篇关于Windows Metro / 8 - 从URL下载图像文件并将其保存到appdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!