8 Replies Latest reply on Oct 16, 2017 11:12 AM by nils.drews

    Binary Data Storage - Seeking working example

    Robert Hanson

      Does anyone have a working example of using the Binary Data Storage API?  I am getting 500 errors when I am posting the binary data to Jive using the temporary URL.  I'm attempting this from a Jive app.

       

      ref: Binary Data Storage Usage

       

      This is the code I'm running in the Jive app.  Unless I'm misunderstanding how to use the mechanism it is fetching the token, and then trying to POST data to the URL.  This is however resulting in a 500 error when posting the content.

       

      osapi.jive.binary.token({
          duration : 10,
          name : 'readme.txt',
          verb : 'POST'
      }).execute(function(response) {
      
          var fd = new FormData();
          var blob = new Blob(["Hello World"], {type : 'text/plain'});
          fd.append('file', blob, "readme.txt");
        
          $.ajax({
            url: response.url,
            data: fd,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function(data){
              alert(data);
            }
          });
      });
      
      

       

      Any ideas would be appreciated.

        • Re: Binary Data Storage - Seeking working example
          Robert Hanson

          Solved.

           

          The important bit is that when you upload the content to Jive that the content type isn't mixed/multipart (aka form data), it is just the binary content.  That means you only send the Blob object and not a FormData object, and you set the content type to application/octet-stream.

           

          // get a temp BDS token to POST the attachment
          osapi.jive.binary.token({duration : 10, name : 'readme.txt', verb : 'POST'
          }).execute(function(response) {
          
              // temp URL to POST the attachment
              var postUrl = response.url;
          
              // create a Blob (file-api) with the file content
              var blob = new Blob(["Hello World"], {type : 'text/plain'});
              // fd.append('file', blob, "readme.txt");
          
              // use the temp token to post the content with jQuery.
              // important: this is different from a upload, the content type isn't a mixed/multipart.
              $.ajax({
                url: postUrl,
                data: blob,
                processData: false,
                contentType: 'application/octet-stream',
                type: 'POST',
                success: function(data){
          
                  // fetch a new BDS token to GET the attachment
                  osapi.jive.binary.token({name: 'readme.txt', verb: 'GET'})
                  .execute(function(response) {
               
                      // temp URL to GET the attachment
                      var getUrl = response.url;
                   
                      // create a new Discussion object and add the attachment URL
                      var attachment = {doUpload: true, "url": getUrl};
                      var discussion = {
                          content: {type: 'text/html', text: 'A Document'},
                          subject: 'Some Discussion',
                          attachments: [attachment]};
                   
                      // use the JS API to create the Discussion and attachment
                      osapi.jive.corev3.discussions.create(discussion)
                      .execute(function (response) { console.log(response); });
          
                  });
               
                }
              });
          });
          
          1 person found this helpful