CloudKit JS saveRecords with asset field for png from base64string

I want to save a CK record with a png-image as data into an asset field. The image is representated by a base64-string variable in the javascript. Have given up after one week searching for an answer.

Accepted Reply

As documented here, you need to pass a Blob to saveRecords method. Assuming the base64 string has the content type you can achieve this like:

fetch(base64String)
  .then((response) => response.blob())
  .then((blob) =>
    database.saveRecords({
      recordType: RECORD_TYPE_NAME,
      fields: {
        [ASSET_FIELD_NAME]: { value: blob },
      },
    })
  );

If the base64 is missing the content type, you can prepend it like data:image/png;base64,${base64String};

Replies

As documented here, you need to pass a Blob to saveRecords method. Assuming the base64 string has the content type you can achieve this like:

fetch(base64String)
  .then((response) => response.blob())
  .then((blob) =>
    database.saveRecords({
      recordType: RECORD_TYPE_NAME,
      fields: {
        [ASSET_FIELD_NAME]: { value: blob },
      },
    })
  );

If the base64 is missing the content type, you can prepend it like data:image/png;base64,${base64String};

Thank you very much, this helped me to fulfill my vision!