LouisDev
LouisDev

Reputation: 53

WL.download with multiple files (OneDrive API)

I'm trying to implement a OneDrive picker. The user can select his files and then, when saving, i can get these files and download them.

I follow the OneDrive API Documentation, and i get this :

WL.init({ client_id: clientId, redirect_uri: redirectUri });
WL.login({ "scope": "wl.skydrive wl.signin" }).then(
   function(response) {
       openFromSkyDrive();
   },
   function(response) {
       log("Failed to authenticate.");
   }
);
function openFromSkyDrive() {
   WL.fileDialog({
       mode: 'open',
       select: 'single'
   }).then(
       function(response) {
           log("The following file is being downloaded:");
           log("");

           var files = response.data.files;
           for (var i = 0; i < files.length; i++) {
               var file = files[i];
               log(file.name);
               WL.download({ "path": file.id + "/content" });
           }
       },
       function(errorResponse) {
           log("WL.fileDialog errorResponse = " + JSON.stringify(errorResponse));
       }
   );
}

function log(message) {
    var child = document.createTextNode(message);
    var parent = document.getElementById('JsOutputDiv') || document.body;
    parent.appendChild(child);
    parent.appendChild(document.createElement("br"));
}

In the select options, you can set 'single' or 'multi' to permit to the user to select one or more files from the picker. But when i try to set 'multi', the WL.download method only work for the last file.

Thanks for help !

ps: i didn't found real solution on stackoverflow or any forum

Upvotes: 1

Views: 876

Answers (1)

daspek
daspek

Reputation: 1474

This is a quirk with the WL.Download() function. It creates a hidden iframe to execute the download, but it uses the same iframe for all the downloads it does. So if you queue up two downloads in quick succession, it will navigate the iframe twice and you'll only end up actually downloading the last file. WL.Download() does not expose when a download is complete, so you can't simply wait for one to finish before starting the next.

Unfortunately, the code sample is a bit misleading, putting the WL.Download() calls in a for-loop. We've taken note of these issues.

In the meantime, to unblock yourself, you can get the download URL from the 'file.source' property and initiate the download yourself.

Upvotes: 3

Related Questions