Uploading an Object Body

By uploading a file as an object body, you can associate and manage the file with a KiiObject.

The maximum size of an object body that the Kii Cloud SDK for JavaScript can handle is 100 MB. For the server stability, we kindly ask you to finish one upload request in about one minute. XMLHttpRequest Level 2, FileReader and Blob support are required to upload object body.

In v2 series of JavaScript SDK, you can check the upload 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 upload feature in server code.

The following sample code shows how to upload an object body after KiiObject creation.

  • // Create a KiiObject in an application-scope bucket.
    var object = Kii.bucketWithName("AppBucket").createObject();
    
    // Save the KiiObject.
    object.save().then(
      function(theObject) {
        // Define a Blob object. You can get a Blob object from an input to an HTML form.
        var srcData = new Blob(["Hello Blob"], {type: "text/plain"});
    
        // Start uploading.
        return theObject.uploadBody(srcData).then(
          function(theObject) {
          // Upload succeeded.
         }
        ).catch(
          function(error) {
            var theObject = error.target;
            var errorString = error.message;
            // Handle the error.
          }
        )
      }
    );
  • // Create a KiiObject in an application-scope bucket.
    var object = Kii.bucketWithName("AppBucket").createObject();
    
    // Save the KiiObject.
    object.save().then(
      function(theObject) {
        // Define a Blob object. You can get a Blob object from an input to an HTML form.
        var srcData = new Blob(["Hello Blob"], {type: "text/plain"});
    
        // Start uploading.
        return theObject.uploadBody(srcData, {
          progress: function (oEvent) {
            if (oEvent.lengthComputable) {
              // Get the upload progress. You can update the progress bar with this function.
              var percentComplete = oEvent.loaded / oEvent.total * 100;
              console.log("upload percentComplete: " + percentComplete);
            }
          }
        });
      }
    ).then(
      function(theObject) {
        // Upload succeeded.
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • // Create a KiiObject in an application-scope bucket.
    var object = Kii.bucketWithName("AppBucket").createObject();
    
    // Save the KiiObject.
    object.save({
      success: function(theObject) {
        // Define a Blob object. You can get a Blob object from an input to an HTML form.
        var srcData = new Blob(["Hello Blob"], {type: "text/plain"});
    
        // Start uploading.
        theObject.uploadBody(srcData, {
          progress: function (oEvent) {
            if (oEvent.lengthComputable) {
              // Get the upload progress. You can update the progress bar with this function.
              var percentComplete = oEvent.loaded / oEvent.total * 100;
              console.log("upload percentComplete: " + percentComplete);
            }
          },
          success: function(theObject) {
            // Upload succeeded.
          },
          failure: function(theObject, errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

Here is what is happening in the sample code:

  • Set key-value pairs (e.g., file name, file size, and the availability of an object body) in a KiiObject as needed.
  • Call the save() method to create a new KiiObject on KiiCloud.
  • Create a blob for the target file.
  • Start uploading by calling the uploadBody() method.
  • (For SDK v2 series,) the progress callback will be called as the upload progresses. It can reach 100% on the first call if the data size is small.

You need to create a KiiObject with the save() method prior to uploading its object body with the uploadBody() method.

Set the content type in a form of "type/subtype". The content type sent to Kii Cloud will be used when the object body is downloaded and when the object body is published and viewed on the browser.