2012-04-03 50 views
2

我正嘗試使用JavaScript在SkyDrive上創建一個新文件。在SkyDrive上創建文件

我到目前爲止最接近的是創建一個文件,但沒有任何內容。

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

     } 

有誰知道如何在SkyDrive上創建一個新文件並傳遞一個字符串作爲文件內容。

我也使用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); } 
      });  

這只是返回一個內部服務器錯誤試圖和讓我很無奈:)

任何人?

+0

當您對用戶進行身份驗證以獲取訪問權限時,您使用的範圍是什麼? – Prashant 2013-04-29 08:19:16

回答

0

很抱歉回覆舊線程。

下面的代碼如何?

它創建一個名爲'hello.txt'的文件,其中包含字符串'Hello,world!'在您的SkyDrive文件夾中。

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); 
}); 

順便說一句,This thread也可能幫助你。