Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

JavaScript
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);
            }
        );
    }
Posted
Updated 3-Dec-14 20:12pm
v2

1 solution

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).

JavaScript
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('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;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900