Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download and save a picture from a url Universal windows app

Tags:

c#

xaml

uwp

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

like image 530
Sebastian Avatar asked Mar 14 '23 17:03

Sebastian


1 Answers

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

like image 200
Sebastian Avatar answered Apr 26 '23 07:04

Sebastian