locked
[WPSL][C#] How to download a collection of images one at a time with WebClient RRS feed

  • Question

  • Hi. I'm trying to download, lets say three, images one by one and I get an error no concurrent I/O with WebClient. I even try to download the next image in the readcompleted event of WebClient. Can you show me a save and simple method to download an array (use string[] images for simplicity) where the string array contains the URLs of each image location. Thank you in advance
    Sunday, September 20, 2015 2:11 PM

Answers

  • Maybe you can use the background transfer service

    https://msdn.microsoft.com/en-us/library/windows/apps/hh202959(v=vs.105).aspx

    Sunday, September 20, 2015 2:50 PM

All replies

  • Maybe you can use the background transfer service

    https://msdn.microsoft.com/en-us/library/windows/apps/hh202959(v=vs.105).aspx

    Sunday, September 20, 2015 2:50 PM
  • I customized the methods to the following...
    private bool AddBackgroundServiceRequest(string name)
            {
                try
                {
                    // Check to see if the maximum number of requests per app has been exceeded.
                    if (BackgroundTransferService.Requests.Count() >= 25)
                    {
                        // Note: Instead of showing a message to the user, you could store the
                        // requested file URI in isolated storage and add it to the queue later.
                        return false;
                    }
    
                    Uri transferUri = new Uri(Uri.EscapeUriString(name), UriKind.RelativeOrAbsolute);
    
    
                    // Create the new transfer request, passing in the URI of the file to 
                    // be transferred.
                    BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
    
                    // Set the transfer method. GET and POST are supported.
                    transferRequest.Method = "GET";
    
                    // Make sure that the required "/pictures" directory exists
                    // in isolated storage.
                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isoStore.DirectoryExists("/pictures"))
                        {
                            isoStore.CreateDirectory("/pictures");
                        }
                    }
    
                    // Get the file name from the end of the transfer URI and create a local URI 
                    // in the "transfers" directory in isolated storage.
                    string downloadFile = name.Substring(name.LastIndexOf("/") + 1);
                    MessageBox.Show(downloadFile);
                    Uri downloadUri = new Uri("pictures/" + downloadFile, UriKind.RelativeOrAbsolute);
                    transferRequest.DownloadLocation = downloadUri;
    
                    // Pass custom data with the Tag property. In this example, the friendly name
                    // is passed.
                    transferRequest.Tag = downloadFile;
    
                    transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
    
                    // Add the transfer request using the BackgroundTransferService. Do this in 
                    // a try block in case an exception is thrown.
                    try
                    {
                        BackgroundTransferService.Add(transferRequest);
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show("Unable to add background transfer request. " + ex.Message);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Unable to add background transfer request.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Add background tranfer request error:\n" + ex.Message);
                    return false;
                }
                return true;
            }
            

    so that I can just pass the image URL to the method like so
    private void DownloadMyFriendsImages()
            {
                try
                {
                    List<String> lstMyFriends = null;
                    if (readSetting("lstMyFriends") != null)
                    {
                        lstMyFriends = (List<String>)readSetting("lstMyFriends");
                    }
                    else
                    {
                        return;
                    }
    
                    //create a list in case background transfer service is full
                    List<String> lstPendingImages = null;
                    if (readSetting("lstPendingImages") != null)
                    {
                        lstPendingImages = (List<String>)readSetting("lstPendingImages");
                    }
                    else
                    {
                        lstPendingImages = new List<string>();
                    }
                            
                    foreach(string name in lstMyFriends)
                    {
                        this.Dispatcher.BeginInvoke(delegate()
                        {
                            txtDebug.Text = "Adding " + name + ".jpg to background tranfer service for downloading";
                        });
                        bool bln = AddBackgroundServiceRequest("http://www.iclips.co.za/uploads/" + name + ".jpg");
                        if (!bln)
                        {
                            lstPendingImages.Add("http://www.iclips.co.za/uploads/" + name + ".jpg");
                        }
                    }
                    if (lstPendingImages.Count > 0)
                    {
                        saveObject("lstPendingImages", lstPendingImages);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Download Friends Images:\n" + ex.Message);
                }
            }
            

    but now I get an error and the message is DownloadLocation. Can you see why?
    Thursday, September 24, 2015 12:44 PM