Create file on SkyDrive

Go To StackoverFlow.com

2

I'm trying to create a new file on SkyDrive using JavaScript.

The closest thing I have so far is to create a file, but without any content.

function upload() {
            WL.api({
                path: "me/skydrive/files/testfile8.txt",
                method: "PUT",
                body: "Some file content"
            }, function (response) {onError(response) });

                function onError(response) {

                $("#status").html(response.error) 
                }

        }

Does anybody know how to create a new file on SkyDrive and pass a string as the file contents.

I have also tried using Ajax

  $.ajax({                   
                type : "PUT",
                url: "https://apis.live.net/v5.0/me/skydrive/files/HelloWorld.txt?access_token=" + ACCESS_TOKEN,
                data: "SOMEDATA",                    
                processData: false,                                        
                success: function() { alert('Success!' );},
                error: function(a,b,c) { alert('Error!' + a + b + c); }
            });      

This just returns a internal server error and leaves me pretty helpless :)

Anybody?

2012-04-03 22:30
by seesharper
When you are authenticating the user to get the access toke, what are scopes you have used - Prashant 2013-04-29 08:19


0

Sorry to reply to an old thread.

How about the following code?

It creates a file called 'hello.txt' that contains the string 'Hello, world!' in your SkyDrive folder.

var CLIENT_ID = '!!!!!!!!CLIENT ID!!!!!!!!';
var REDIRECT_URI = '!!!!!!!REDIRECT URI!!!!!!!';
var filename = 'hello.txt';
var content = 'Hello, world!';
var access_token = '';
WL.init({
    client_id: CLIENT_ID,
    redirect_uri: REDIRECT_URI
}).then(function (response) {
    return WL.login({scope: ['wl.signin', 'wl.skydrive', 'wl.skydrive_update']});
}).then(function (response) {
    access_token = response.session.access_token;
    return WL.api({path: 'me/skydrive'});
}).then(function (response) {
    var url = response.upload_location + filename + '?access_token=' + access_token;
    var xhr = new XMLHttpRequest();
    xhr.open('PUT', url);
    xhr.onload = function () {
        alert('success:', xhr.responseText);
    };
    xhr.onerror = function (error) {
        alert('XHR error:', xhr.responseText);
    };
    xhr.send(new Blob([content]));
}, function (error) {
    alert('error:', error);
});

BTW, This thread may also help you.

2013-11-17 08:31
by seikichi