Downloading an Object Body

When you get a KiiObject, its object body is not downloaded together. You need to download an object body separately. XMLHttpRequest Level 2, FileReader and Blob support are required to download object body.

In v2 series of JavaScript SDK, you can check the download progress using progress callback.
(This feature is not available in SDK v3 series because the callback itself is obsoleted.)

You cannot use the object body download feature in server code.

The following sample code shows how to download an object body from a KiiObject.

  • // Instantiate the target KiiObject.
    var object = KiiObject.objectWithURI('Set the URI of an existing KiiObject here');
    
    // Start downloading.
    object.downloadBody().then(
      function(params) {
        var theObject = params[0];
        var bodyBlob = params[1];
        var contentType = bodyBlob.type;
        // Do something.
        // The object body is downloaded as bodyBlob.
        // The type attribute contains the content type managed in Kii Cloud.
        // object.getBodyContentType() returns the same type;
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • // Instantiate the target KiiObject.
    var object = KiiObject.objectWithURI('Set the URI of an existing KiiObject here');
    
    // Start downloading.
    object.downloadBody({
      progress: function (oEvent) {
        if (oEvent.lengthComputable) {
          var percentComplete = oEvent.loaded / oEvent.total * 100;
          // Get the download progress. You can update the progress bar with this function.
        }
      }
    }).then(
      function(params) {
        var theObject = params[0];
        var bodyBlob = params[1];
        var contentType = bodyBlob.type;
        // Do something.
        // The object body is downloaded as bodyBlob.
        // The type attribute contains the content type managed in Kii Cloud.
        // object.getBodyContentType() returns the same type;
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • // Instantiate the target KiiObject.
    var object = KiiObject.objectWithURI('Set the URI of an existing KiiObject here');
    
    // Start downloading.
    object.downloadBody({
      progress: function (oEvent) {
        if (oEvent.lengthComputable) {
          var percentComplete = oEvent.loaded / oEvent.total * 100;
          // Get the download progress. You can update the progress bar with this function.
        }
      },
      success: function(theObject, bodyBlob) {
        // Do something.
        // The object body is downloaded as bodyBlob.
        // The type attribute contains the content type managed in Kii Cloud.
        // object.getBodyContentType() returns the same type;
        var contentType = bodyBlob.type;
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

(For SDK v2 series,) the progress callback will be called as the download progresses. It can reach 100% on the first call if the data size is small.