You can let users upload, display, and download files or images in a MDriven Turnkey application by exposing Blob attributes through a ViewModel; this guide is for MDriven Designer users who model those attributes and views.
Overview
A Blob is an attribute that stores file or image bytes. In Turnkey, how a Blob is rendered depends on the Blob type and the ViewModel column settings:
| Goal | Model and ViewModel setup | Result in Turnkey |
|---|---|---|
| Upload a file | Add a writable Blob attribute to a class and expose it in a ViewModel as a non-read-only column. | An upload button. Its text comes from the ViewModel column name. |
| Download a file | Expose the Blob in a ViewModel and set the Blob ViewModel column to isStatic. Add the BlobDownloadLink tagged value when you want a download link. Store matching filename and content-type attributes.
|
A downloadable Blob with the correct file name and content type. |
| Upload an image | Add a Blob attribute and set its BlobType property to Image. Expose it as a writable ViewModel column.
|
An image upload control. |
| Show an existing image | Expose the image Blob as a read-only expression column, or set its ViewModel column to isStatic.
|
The image is presented without an upload control. |
Upload a file
Model the file attributes
- In MDriven Designer, add a Blob attribute to the class that owns the file. For example, add
Attachmentto aDocumentclass. - Add a String attribute named
Attachment_FileNameto the same class. Turnkey sets this attribute to the uploaded file name. - If you will provide a download link, add a String attribute named
Attachment_ContentTypeas well. Store the uploaded file's content type in this attribute so Turnkey can create the download link correctly.
The filename and content-type attribute names are based on the Blob attribute name. For a Blob named Attachment, use Attachment_FileName and Attachment_ContentType.
| Example class attribute | Type | Purpose |
|---|---|---|
Attachment
|
Blob | The uploaded file bytes. |
Attachment_FileName
|
String | The uploaded file name, such as proposal.pdf.
|
Attachment_ContentType
|
String | The file content type used for a correct download link. |
A Blob backing attribute can also be a derived settable attribute. Use this when assigning the uploaded bytes must run model logic, such as processing the incoming file before it is retained.
Expose the Blob for upload
- Create or open the ViewModel that edits the object, for example a
Documentedit ViewModel. - Add a ViewModel column for the Blob attribute, for example
Attachment. - Ensure that the column is not read-only.
- Publish or run the Turnkey application and open the view.
Turnkey renders the writable Blob column as an upload button. The button text is based on the ViewModel column name. For example, a column named Attachment produces an upload control identified by that column name.
Download a file
To present a Blob as a file download, configure the Blob column as a static value in the ViewModel and provide the file metadata attributes described above.
- Add the Blob attribute to the ViewModel.
- Select the ViewModel column containing the Blob and set
isStatic. - Add the
BlobDownloadLinktagged value to create a download link for that Blob. - Include and populate the matching
<BlobName>_FileNameand<BlobName>_ContentTypeString attributes.
For example, when Document.Attachment contains the bytes for a PDF, Attachment_FileName can contain proposal.pdf and Attachment_ContentType stores its content type. Turnkey uses this metadata when it creates the download link.
Upload and display images
Mark the Blob as an image
- Add a Blob attribute to the class, for example
EmployeePhotoon anEmployeeclass. - In the attribute properties area in MDriven Designer, set
BlobTypetoImage. - Add the attribute to a ViewModel as a writable column to allow image upload.
The BlobType setting tells Turnkey that the Blob is an image rather than a general file.
Display an image without allowing upload
Use either of these ViewModel approaches when users must see an image but must not replace it:
- Make the image column a read-only expression.
- Set
isStaticfor the image ViewModel column.
For example, an employee-details ViewModel can show EmployeePhoto as a static column while a separate employee-edit ViewModel exposes it as writable.
Image data reached through an expression
When a ViewModel column uses an expression to reach the byte array, Turnkey cannot infer whether the value is an image or a general file. Add this tagged value to the ViewModel column:
Eco.BlobType=Image
This tagged value applies to the ViewModel column, not to the underlying class attribute. Use it, for example, when an expression resolves an employee photo through a related object instead of directly exposing the image Blob attribute.
Store blobs outside the database
You can use BlobStorage when you do not want files and images stored in the database. This is an alternative storage choice; the Blob attributes and ViewModel presentation described on this page still define how users work with the file or image in Turnkey.
Upload multiple files
A normal writable Blob column uploads one file to its target Blob. If a user must select and upload several files in one operation, use the dedicated multiple file upload component. That component creates a target object for each file and uploads the files in sequence.
Implementation note for custom clients
Turnkey's browser client uses JavaScript generated from TypeScript to upload files. The Turnkey server endpoint used for multipart upload is named UploadFileMultiPartController. If you build a custom client, inspect the Turnkey JavaScript or TypeScript implementation and reproduce the required upload behavior there.
Checklist
Before testing, confirm the following:
- The data-owning class has a Blob attribute.
- The upload ViewModel exposes that Blob in a column that is not read-only.
- A file Blob has a matching
<BlobName>_FileNameString attribute. - A downloadable Blob also has a matching
<BlobName>_ContentTypeString attribute, its ViewModel column hasisStatic, and the column has theBlobDownloadLinktagged value. - An image Blob has
BlobTypeset toImage, or an expression-based image column hasEco.BlobType=Image. - Multi-file requirements use the multiple file upload component rather than one standard Blob upload column.
