Windows Metro / 8 - 从URL下载图像文件并将其保存到appdata [英] Windows Metro/8 - download image file from URL and save it to appdata

查看:88
本文介绍了Windows Metro / 8 - 从URL下载图像文件并将其保存到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-d7833f61

Also, 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆