【问题标题】:SkyDrive API Displaying a fileSkyDrive API 显示文件
【发布时间】:2013-11-25 10:05:41
【问题描述】:

我正在使用 Skydrive API,我希望我的用户能够打开一个关于文件的视图,您可以在其中编辑它(与您在 Skydrive 网络上时我们可以看到的文件视图相同)页)。

它可能有 WL 功能,但我找不到。另一种解决方案是让我获取视图页面的 URL 并使用 javascript 在新窗口中打开它。

【问题讨论】:

    标签: javascript api onedrive


    【解决方案1】:

    我已经使用 SkyDrive 及其 API 实现了这个解决方案。您也可以在Microsoft's Interactive Live SDK 在线工具中试用此脚本。关键是为您要打开的文件获取 SkyDrive 的重定向链接。 Get api 的 json 结果中的每个文件都会返回此重定向链接。

    处理步骤:

    1. 初始化 Windows Live jscript api 客户端
    2. 使用 Windows Live (skydrive) OAuth 进行身份验证
    3. GetFiles: 获取您 SkyDrive 帐户中所有文件的列表。这可以根据您的需要进行调整,并专注于使用您的 SkyDrive 帐户获取特定文件夹的列表
    4. onFilesComplete: 遍历 json 响应,查找您要打开的 type='file' 和文件名的项目。在这种情况下,文件名“robots.txt”
      • 显示找到的文件的详细信息
      • 使用找到的文件的“链接”属性,在新窗口浏览器窗口中打开 url。这将使用 SkyDrive 默认操作打开文件。对于已知的文件类型,例如代码文件,这将在 SkyDrive 的在线文件编辑器中打开它们。否则,默认操作将是下载找到的文件

    示例代码:

    WL.init({ client_id: clientId, redirect_uri: redirectUri });
    
    WL.login({ "scope": "wl.skydrive" }).then(
        function(response) {
            getFiles();
        },
        function(response) {
            log("Could not connect, status = " + response.status);
        }
    );
    
    function getFiles() {
        var files_path = "/me/skydrive/files";
        WL.api({ path: files_path, method: "GET" }).then(
            onGetFilesComplete,
            function(response) {
                log("Cannot get files and folders: " +
                    JSON.stringify(response.error).replace(/,/g, ",\n"));
            }
        );
    }
    
    function onGetFilesComplete(response) {
        var items = response.data;
        var foundFolder = 0;
        for (var i = 0; i < items.length; i++) {
            if (items[i].type === "file" &&
                items[i].name === "robots.txt") {
                log("Found a file with the following information: " +
                    JSON.stringify(items[i]).replace(/,/g, ",\n"));
                foundFolder = 1;
                //open file in a new browser window
                window.open(items[i].link);
                break;
            }
        }
    
        if (foundFolder == 0) {
            log("Unable to find any file(s)");
        }
    }
    
    function log(message) {
        var child = document.createTextNode(message);
        var parent = document.getElementById('JsOutputDiv') || document.body;
        parent.appendChild(child);
        parent.appendChild(document.createElement("br"));
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多