以编程方式在onedrive中创建文件? [英] Create file in onedrive programatically ?

查看:77
本文介绍了以编程方式在onedrive中创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想从javascript创建一个docx或pptx文件到我的onedrive帐户。问题是可能会出现类型必须是文件夹专辑的错误。怎么了?



*注意:我成功创建了一个文件夹。



Hi. I want to create a docx or pptx file from javascript to my onedrive account. The problem is thati get an error that says "The type must be folder or album". What Is wrong?

*Note: I successfully created a folder.

function create_docx()
    {
        WL.login({
            scope: "wl.skydrive_update"
        }).then(
            function (response) {
                WL.api({
                    path: "me/skydrive",
                    method: "POST",
                    body: {
                        "name": "myfile.docx",
                        "description": "A new word doc",
                    },
                    name: "myfile.docx",
                    type: "file"
                }).then(
                    function (response) {
                        log("Created file. Name: " + response.name + ", ID: " + response.id);
                    },
                    function (responseFailed) {
                        log("Error calling API: " + responseFailed.error.message);
                    }
                );
            },
            function (responseFailed) {
                log("Error signing in: " + responseFailed.error_description);
            }
        );
    }

推荐答案

嗨再次。因为没有人回答我,我认为这有点困难。我整天搜索这个问题,并在决赛中找到了一些东西(感谢msdn门户网站和论坛)。我成功了让它工作,我在这里发布我的解决方案。我还发现javascript库WL不支持直接在onedrve帐户中创建文件,但REST服务确实如此,我只是使用了与AJAX的REST服务。 (我不知道是否是最好的解决方案,但对我来说它是有效的。)



Hi again. Because nobody answer me I suppose that is a little bit difficult. I searched all day long about this problem and in the final I found something (thanks to msdn portal and forum). I succeded to make it work and I post my solutin right here. I also found that the javascript library WL dows not support directly to create a file in the onedrve account but REST services does so I just used REST services with AJAX. (I don't know if is the best solution but for me it's working).

function create_docx()
    {
        WL.login({
            scope: ["wl.signin", "wl.basic", "wl.skydrive", "wl.skydrive_update"]
        }).then(
            function (response) {


.ajax({
type:' POST'
contentType: multipart / form-data; boundary = A300x
processData: false
url:' https://apis.live.net/v5 .0 / me / skydrive / files?access_token =' + response.session.access_token,
data:createUploadRequestBody(),
success: function (){alert(' 成功!'); },
错误: function (){alert(response.error_description); },
});
});
}

function createUploadRequestBody(){
var body = - A300x \\\\ n
+ Content-Disposition:form-data; name = \file \; filename = \csm.docx \ \\\\ n
+ Content-Type:application / octet- stream \\\\ n
+ \\\\ n
+ 这是一些内容\\\\ n
+ \\\\ n
+ - A300x - \\\\ n;
return body;
}
.ajax({ type: 'POST', contentType: "multipart/form-data; boundary=A300x", processData: false, url: 'https://apis.live.net/v5.0/me/skydrive/files?access_token=' + response.session.access_token, data: createUploadRequestBody(), success: function () { alert('Success!'); }, error: function () { alert(response.error_description); }, }); }); } function createUploadRequestBody() { var body = "--A300x\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"csm.docx\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "This is some content\r\n" + "\r\n" + "--A300x--\r\n"; return body; }


这篇关于以编程方式在onedrive中创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆