下载并从URL万能的Windows应用程序保存图片 [英] Download and save a picture from a url Universal windows app

查看:143
本文介绍了下载并从URL万能的Windows应用程序保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码,立即下载的图片形成一个远程URL并保存到本地存储文件夹

 
{
VAR rootFolder =等待ApplicationData.Current.LocalFolder.CreateFolderAsync(MyAppName\\CoverPics,CreationCollisionOption.OpenIfExists);

VAR coverpic_file =等待rootFolder.CreateFileAsync(文件名,CreationCollisionOption.FailIfExists);

{
变种的HttpWebRequest = HttpWebRequest.CreateHttp(coverUrl);
HttpWebResponse响应=(HttpWebResponse)等待httpWebRequest.GetResponseAsync();
流resStream = response.GetResponseStream();使用
(VAR流=等待coverpic_file.OpenAsync(FileAccessMode.ReadWrite))
{
等待resStream.CopyToAsync(stream.AsStreamForWrite());
}
response.Dispose();
}
抓//保存图片
{
中保存=虚假的任何异常happend;
}
}

{
$ //https://msdn.microsoft.com/en-us/library/windows/apps/br227250.aspx如果文件已经存在
中保存=对b $ b //引发一个异常;
}

此代码是为我工作在大多数情况下,但我注意到,为几张照片图像没有完全下载。



我在foreach循环内,并且在一个单一的去callling在异步块此功能进行更多TAHN 100张图像他们结束几个是失败的下载



[任我可以看到一些无效的文件是越来越创建



或部分图像仅在该地区的下载和休息,我可以看到一个黑色的色块[看起来就像图像被损坏。



所有图像的大小只有不到1 MB



有人可以帮我优化这个代码或指出在代码中的错误,所以我可以能够完全下载的所有图像


< DIV CLASS =h2_lin>解决方案

我没有看到任何错误在我的代码。但是,试图下载的一些不同的方式和保存文件我的代码看起来像这样以及

 
{$ B后$ b HttpClient的客户端=新的HttpClient(); //创建HttpClient的
字节[]缓冲=等待client.GetByteArrayAsync(coverUrl);使用//下载文件
(流流=等待coverpic_file.OpenStreamForWriteAsync())
stream.Write(缓冲,0,buffer.Length); //保存
}

{
中保存= FALSE;
}

和这个代码是没有造成任何问题,所有图像都完全下载做工精细和黑块的图像上没有更多的问题。



如果任何人能指出我的第一个代码的差异将非常有助于理解错误的原因


I am using the below code to downlaod the picture form a remote url and save to Local storage folder

        try
        {
            var rootFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync( "MyAppName\\CoverPics", CreationCollisionOption.OpenIfExists);

            var coverpic_file = await rootFolder.CreateFileAsync(filename, CreationCollisionOption.FailIfExists);
            try
            {
                var httpWebRequest = HttpWebRequest.CreateHttp(coverUrl);
                HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
                Stream resStream = response.GetResponseStream();
                using (var stream = await coverpic_file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await resStream.CopyToAsync(stream.AsStreamForWrite());
                }
                response.Dispose();
            }
            catch //any exceptions happend while saving the picture
            {
                saved = false;
            }
        }
        catch
        {
            //https://msdn.microsoft.com/en-us/library/windows/apps/br227250.aspx 
            //Raise an exception if file already present 
            saved = true;
        }

This code is working for me in most of the cases , but i noticed that for few pictures the image is not downloading completely.

I am callling this function in an async block for more tahn 100 images in a single go inside the foreach loop and in the end few of them are failed downloads

[ Either i can see some invalid file is getting created

or part of image only in downloading and rest of the area i can see a black colour block [ looks like image is corrupted].

Size of all images is less than 1 MB only

Can some one help me to optimize this code or point out the mistake in code so i can able to download all the images completely

解决方案

I am not seeing any error in my code. But after trying some different ways of downloading and saving a file my code looks like this and

 try
            {
                HttpClient client = new HttpClient(); // Create HttpClient
                byte[] buffer = await client.GetByteArrayAsync(coverUrl); // Download file
                using (Stream stream = await coverpic_file.OpenStreamForWriteAsync())
                    stream.Write(buffer, 0, buffer.Length); // Save
            }
            catch
            {
                saved = false;
            }

And this code is working fine without causing any issues All images are downloading completely and no more issues of black block on images.

If any one can points out the difference with my first code will be really helpful to understood the reason for error

这篇关于下载并从URL万能的Windows应用程序保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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