Uploading an Object Body

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

To upload a file as an object body, use the UploadBody() method like the following example:

// Create a KiiObject in an application-scope bucket.
KiiObject obj = Kii.Bucket("AppBucket").NewKiiObject();

// Set key-value pairs.
obj ["score"] = 987;
obj ["mode"] = "easy";
obj ["premiumUser"] = false;

// Save the KiiObject.
obj.Save((KiiObject savedObj, Exception e) => {
  if (e != null)
  {
    // Handle the error.
    return;
  }

  // Specify a file to upload.
  FileStream file = new FileStream("myImage.jpg", FileMode.Open);

  // Start uploading.
  obj.UploadBody("image/jpeg", file, (KiiObject uploadedObj, Exception e2) => {
    if (e2 != null)
    {
      // Handle the error.
      return;
    }
    // The object body has been successfully uploaded.
  },
  (KiiObject uploadingObj, float progress) => {
    // Handle the progress.
  });
});

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 Kii Cloud.
  • Create a reference to the target file (myImage.jpg).
  • Start uploading by calling the UploadBody() method.

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

The callback method set as the third argumenbt will be called as the upload progresses. The method can reach 100% on the first call if the data size is small.

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.