Reputation: 41
I am currently generating a file on my file system using xlsx
NodeJS library, I also have created empty file on OneDrive using create item
end point (https://graph.microsoft.io/en-us/docs/api-reference/beta/api/item_post_children)
Now, I want to upload content of the file on OneDrive with the content of the file that I have on the file system. I am trying to do this using Upload Item
end point (https://graph.microsoft.io/en-us/docs/api-reference/beta/api/item_uploadcontent) and the request is passing ok, but when I try to open the file with Excel Online I am getting message that the file is corrupted
Anyway, if I upload the file through the OneDrive web interface, everything works fine
Any idea why the content upload not working?
Upvotes: 4
Views: 1830
Reputation: 44
Upload it as binary file. Ruby code for uploading an excel file to onedrive
require 'net/http'
upload_path = 'https://graph.microsoft.com/v1.0/drive/root/children/test_file.xlsx/content'
request = Net::HTTP::Put.new(upload_path, {'Authorization': token })
f= File.new(file_path, "r+b")
request.body = f.read
resp = https.request request
Upvotes: -1
Reputation: 2478
An alternate way maybe is to upload an empty Excel file and update the data using Excel REST API.
Upvotes: 1
Reputation: 135
The contents of the file should be the binary stream of the file being uploaded. I created a sample web app for people to play with the Graph APIs called Visual Graph Explorer. Once you log on to the web app with O365 credentials, you can click on the Files link and upload a file. On the right you will see a log of the requests and responses. If you want to see the full source you can check it out on GitHub.
The code I use for the upload is in JavaScript and runs on the client side.
var f = document.getElementById("inputFile").files[0];
var r = new FileReader();
r.onloadend = function (e) {
var data = e.target.result;
var url = "https://graph.microsoft.com/v1.0/users/" + $scope.upn + "/drive/root/children/" + f.name + "/content";
$http.put(url, data)
.then(function successCallback(response) {
document.getElementById("inputFile").value = "";
}, function errorCallback(response) {
$scope.error = JSON.stringify(response);
$scope.loadingMessage = "";
})
}
r.readAsBinaryString(f);
Upvotes: 2