Versions Compared
Version | Old Version 5 | New Version 6 |
---|---|---|
Changes made by | ||
Saved on |
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Table of Contents |
---|
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:
Code Block |
---|
persistent object CropQualityPicture { blob image; string imageStr; } |
Code Block |
---|
// 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:
Code Block |
---|
// 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 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:
Code Block |
---|
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; |
Code Block |
---|
// 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 reloading the current view.
As an example, see the following action triggered from a frontend submit button:
Code Block |
---|
</submit> <submit label="submit.download_selected" action="downloadSelectedCropQualityPicture"> </submit> |
Code Block |
---|
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; } |
Additional Mentions and References
Excerpt | ||
---|---|---|
| ||
Blob:fromString, Blob:toString, Blob:wrapFromString, Mez:downloadFile |