We would like to upload files to OneDrive.
We have not been able to get that working. It seems OneDrive want a binary stream for the file-content.
The manual we turned to is:
The method we wrote is:
function oneDriveUpload(parent_id,filename,file_content){
application.output(file_content)
var $url = "https://graph.microsoft.com/v1.0/";
var $client = plugins.http.createNewHttpClient();
var $access_token = RefreshToken(_to_app_user$current_user.office_365_refresh_token);
var post_url = '/me/drive/items/'+parent_id+':/'+filename+':/content';
var $post = $client.createPutRequest($url + encodeURI(post_url));
$post.addHeader("Authorization", "Bearer " +$access_token)
$post.addHeader("Content-Type", "text/plain");
// $post.addHeader("Content-Type", "application/json");
// $post.addHeader("Content-Type", "application/octet-stream");
$post.setBodyContent(file_content)
var executeRequest = $post.executeRequest();
return {
'getStatusCode': executeRequest.getStatusCode(),
'getResponseBody': JSON.parse(executeRequest.getResponseBody())
};
// /me/drive/items/{parent-id}:/{filename}:/content
}
With this method the file is being created, but empty.
We have tried several things to get the file_content variable as binary-data, but still did not manage to get the content in the OneDrive file.
With the file-plugin (JSFile.getBytes())we only can get byte-arrays, not binary data.
Is there a plugin available we don’t know of that can create a binary stream we can use to pass the content to a OneDrive-file, or even a OneDrive/Office365 plugin that does all the work, or did anyone come up with another solution to write files to OneDrive ?
Instead of:
post.setBodyContent(file_content)
Use this, where file is a JSFile object.
post.setFile(file) ;
Also, here is a generic method I use:
/**
* @param {String} [endpoint] URL to POST
* @param {Object|plugins.file.JSFIle} data Data to POST
* @param {String} [mimeType] Optional type
* @return { {headers:Object, json:Object, status:Number}}
*
* @properties={typeid:24,uuid:"CA827DFB-63E9-4408-A623-4662A564EBD3"}
*/
function submitJSON_PUT(endpoint, data, mimeType){
if(!endpoint)
endpoint = url
if(!endpoint)
throw "Missing endpoint URL"
if(!mimeType)
mimeType = 'application/json'//we assume JSON
if(data instanceof plugins.file.JSFile)
mimeType = "application/octet-stream"
var httpClient = plugins.http.createNewHttpClient()
var put = httpClient.createPutRequest(endpoint)
if(globalHeaders){
for(var header in globalHeaders){
put.addHeader(header, globalHeaders[header])
}
}
if(mimeType == 'application/json'){
put.addHeader('Content-type',mimeType)
put.addHeader('Accept',mimeType)
put.setBodyContent(JSON_stringify(data))
}
else if(mimeType == "application/octet-stream"){
put.addHeader('Content-type',mimeType)
put.addHeader('Connection',"Keep-Alive")
/** @type {plugins.file.JSFile} */
var file = data
put.setFile(file) ;
}
var resp = put.executeRequest()
return processResponse(resp)
}
Hello Scott, thanks for the answer.
We tried setFile, but the result is still the same: no filecontent so far in the file on the OneDrive cloud.
Question is whether or not the file is really a stream or still a binary array.
Do you use this method for OneDrive yourself, or do you use the method posted for another goal ?
Do maybe have other suggestions to get this working ?
I do use that code, specifically with OneDrive, and have no problems. I use the API to connect directly to OneDrive: https://dev.onedrive.com/getting-started.htm . I don’t go through Microsoft Graph, but I doubt that makes a difference.
In my example, I use the File plugin to ask the user to choose a file on their computer, and that is the JSFile argument that gets passed in.
Hi Scott,
We have tried your method but we’re still aren’t able to get it working.
Is it possible that you can provide us with a test solution for file uploading.
I hope to hear from you soon