这里的问题是调用wl.download({ "path": file.id + "/content" })
存储一些内部状态(除其他外,正在下载的文件及其当前状态)。通过遍历文件列表,该状态实际上被每次调用覆盖。当我尝试一次下载三个文本文件时,实际下载的总是最后一个,而不是前两个。
这里的困难在于下载是以传统方式执行的,即服务器添加Content-Disposition: attachment
到响应标头以强制浏览器下载文件。因此,当下载实际完成时,不可能收到任何类型的通知,这意味着您无法连续执行下载来解决状态问题。
我认为可能有效的一种方法是受到这个问题的启发。根据文档/content?suppress_redirects=true
,如果我们附加到文件的 id ,我们可以获得文件的下载链接。使用这种方法,我们可以设置src
IFrame 的属性并以这种方式下载文件。这可以正常工作,但它只会强制下载由于缺少Content-Disposition: attachment
响应标头而浏览器无法原生显示的文件类型(zip 文件、Exe 文件等)。
以下是我在 Interactive Live SDK 中使用的内容。
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: 'multi'
}).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.api({
path: file.id + "/content?suppress_redirects=true",
method: "GET"
}).then(
function (response) {
var iframe = document.createElement("iframe");
iframe.src = response.location;
iframe.style.display = "none";
document.body.appendChild(iframe);
},
function (responseFailed) {
log("Error calling API: " + responseFailed.error.message);
}
);
}
},
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"));
}