/
Blob Functions

Blob Functions



Convert base64 encoded string to blob


In some cases file data might be represented by base64 encoded strings.

The following can be used to convert from base64 encoded file data strings to blob:

persistent object CropQualityPicture {
	blob image;
	string imageStr;
}
// Convert base64 string to blob
cropQualityPicture.image = Blob:fromString(cropQualityPicture.imageStr);

Note that if the resulting blob is to be used with any frontend widgets, it is recommended that the Blob:wrapFromString built in function is used instead since this will also populate meta fields for the blob that are needed by most frontend widgets that handle blobs.




Convert blob to base64 encoded string

Similarly to the above, blobs can also be converted to base64 encoded strings:

// Convert blob to base64 string
cropQualityPicture.imageStr = Blob:toString(cropQualityPicture.image);




Convert base64 encoded string to a wrapped blob

Since blob fields on objects are automatically accompanied by meta attributes that describe the mime type, file size and file name, an additional conversion is available where a base64 encoded string can be converted to a built-in object that wraps the blob and includes the above mentioned meta fields automatically:

MezBlobResult blobWrapper = Blob:wrapFromString(cropQualityPicture.imageStr, "image/jpeg", "file_name_str");

// This assignment will assign the blob field and also copy the meta fields
cropQualityPicture.image = blobWrapper.blobData;
// Built-in wrapper object
object MezBlobResult {
	blob blobData;

	// blobData_mtype__, blobData_fname__, blobData_size__, are included by default
}




Download a file for a blob

Blobs can also be downloaded as files on the frontend. One method is to use the file browser widget.

Another, more flexible, method is with the Mez:downloadFile built-in function.

By calling the built-in function and providing an object that contains the blob attribute, a file download will trigger when navigating or when reloading the current view.

As an example, see the following action triggered from a frontend submit button:

</submit>
	<submit label="submit.download_selected" action="downloadSelectedCropQualityPicture">
</submit>
DSL_VIEWS downloadSelectedCropQualityPicture() {
	if(selectedCropQualityPicture == null) {
		Mez:alertWarn("alert_warn.no_images_selected");
		return null;
	}

	// Last parameter representing file name is optional 
	// If not specified _fname__ field will be used as the file name
	Mez:downloadFile(selectedCropQualityPicture, "image", getFileName());

	// Any navigation even to the same view will trigger the file download
	return null;
}






Related content