Downloading an Object Body

When you get a KiiObject, its object body is not downloaded together. You need to download an object body separately.

The Kii Cloud SDK supports to download an object body with and without progress callback. To download an object body, your web app needs to support XMLHttpRequest Level 2, FileReader, and Blob. The Titanium SDK is currently not supported.

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({
      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.
      }
    });
  • // 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.
      }
    );

The progress callback will be called as the download progresses. It can reach 100% on the first call if the data size is small.