Download image from json not complete

I have a json that contains a string and an image link. I want to download all of the images.

Code:

question.hasImg = hasimg;
                                if (question.hasImg == "1")
                                {
                                    JsonArray gambar = groupObjectSoal["images"].GetArray();\
                                    foreach (JsonValue groupValueGambar in gambar)
                                    {
                                        string imgSoal = groupValueGambar.GetString();
                                        imgName = System.IO.Path.GetFileNameWithoutExtension(imgSoal);
                                        DownloadGambar(imgSoal);

                                        IReadOnlyList<DownloadOperation> downloads = null;
                                        downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();
                                        int i = 0;

                                        if (downloads.Count > 0)
                                        {
                                            gambardownloading = new string[downloads.Count];
                                            foreach (DownloadOperation download in downloads)
                                            {
                                                // list download tryout
                                                gambardownloading[i] = download.ResultFile.Name;
                                                i++;
                                            }
                                        }
                                    }
                                }
StorageFolder installedLocation = ApplicationData.Current.LocalFolder;
        private async void DownloadGambar(string fileLocation)
        {
            var uri = new Uri(fileLocation);
            var downloader = new BackgroundDownloader();
            StorageFolder library = await installedLocation.CreateFolderAsync("library", CreationCollisionOption.OpenIfExists);
            StorageFolder gambar = await library.CreateFolderAsync("gambar", CreationCollisionOption.OpenIfExists);
            StorageFolder idName = await gambar.CreateFolderAsync(quiz.ID.ToString(), CreationCollisionOption.OpenIfExists);
           StorageFile file = await idName.CreateFileAsync(imgName + ".JPG",
                CreationCollisionOption.ReplaceExisting);
            DownloadOperation download = downloader.CreateDownload(uri, file);
            await GambarStartDownloadAsync(download);
        }
private void GambarProgressCallback(DownloadOperation obj)
        {
            double progress
              = ((double)obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive);
            progress = Math.Round((double)progress, 2);
            if (progress >= 1.0)
            {
                _activeDownload = null;
            }
        }

I'm having a problem, i.e. not all images are downloaded (for example there are 27 images, but only 25 images that were successfully downloaded). How to solve this problem?

1 answer

  • answered 2019-10-08 04:47 lindexi

    I think you can use try-catch in image download method and I only add the succeed download file to list. And I add the thumb which show the download fail to the list to show the the item which download fail.

    And then we can re-try download the file in background and set the image when download successfully.