Get filecontent as binary data

Questions and answers on developing, deploying and using plugins and JavaBeans

Get filecontent as binary data

Postby thijsjan.hoving » Thu Jun 30, 2016 12:32 pm

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:
https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_uploadcontent

The method we wrote is:
Code: Select all
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 ?
thijsjan.hoving
 
Posts: 3
Joined: Thu Jun 30, 2016 11:58 am

Re: Get filecontent as binary data

Postby sbutler » Thu Jun 30, 2016 10:37 pm

Instead of:
Code: Select all
post.setBodyContent(file_content)

Use this, where file is a JSFile object.

Code: Select all
post.setFile(file) ;


Also, here is a generic method I use:
Code: Select all
/**
* @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)
}
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Get filecontent as binary data

Postby thijsjan.hoving » Tue Jul 05, 2016 10:44 am

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 ?
thijsjan.hoving
 
Posts: 3
Joined: Thu Jun 30, 2016 11:58 am

Re: Get filecontent as binary data

Postby sbutler » Tue Jul 05, 2016 2:38 pm

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.
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: Get filecontent as binary data

Postby oscar.balk » Wed Aug 17, 2016 5:34 pm

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
oscar.balk
 
Posts: 2
Joined: Mon Mar 05, 2012 1:40 pm


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 11 guests