How To: Add a File Property
time cockpit supports file attachments to database rows. To store the file's content and metadata you have to extend your model (see also How To: Modify the Data Model). In this chapter we will cover details about how to add a file property to your model and how to use it from within lists and forms.
Adding the Property
FileProperty implements the property holding the file's content. In order to work properly in the user interface you have to create additional properties that store the file's name (FileNameColumn), the MIME type (FileMimeTypeColumn) and the file's size (FileSizeColumn).
The following example shows how to add a file property to the entity Contact
:
clr.AddReference("System.Core")
from System.Linq import Enumerable
from System.Collections.Generic import List
model = Context.GetWritableModel()
contact = model.Entities.Contact
if not contact.Properties.Contains("USR_Avatar"):
# Avatar file name
fileNameProperty = TextProperty({
"Name": "USR_AvatarFileName", "InvariantFriendlyName":"Avatar File Name",
"MaxStorageSize" : 256, "IsNullable" : True, "IsDefaultDisplayProperty" : False,
"DefaultValueExpression" : "\"\"", "Ownership" : Ownership.User
})
contact.Properties.Add(fileNameProperty)
# Avatar file mime type
mimeTypeProperty = TextProperty({
"Name": "USR_AvatarMimeType", "InvariantFriendlyName":"Avatar Mime Type",
"MaxStorageSize" : 256, "IsNullable" : True,
"IsDefaultDisplayProperty" : False, "DefaultValueExpression" : "\"\"",
"Ownership" : Ownership.User
})
contact.Properties.Add(mimeTypeProperty)
# Avatar file size
fileSizeProperty = NumericProperty({
"Name": "USR_AvatarFileSize", "InvariantFriendlyName":"Avatar File Size",
"Precision" : 18, "Scale" : 0, "IsNullable" : True, "FormatPattern": "#,###",
"IsDefaultDisplayProperty" : False, "DefaultValueExpression" : "0",
"Ownership" : Ownership.User
})
contact.Properties.Add(fileSizeProperty)
# Avatar file property
allowedMimeTypes = List[String]()
allowedMimeTypes.Add("image/jpeg")
allowedMimeTypes.Add("image/png")
allowedExtensions = List[String]()
allowedExtensions.Add(".png")
allowedExtensions.Add(".jpeg")
allowedExtensions.Add(".jpg")
fileProperty = FileProperty({
"Name": "Avatar",
"InvariantFriendlyName": "Avatar",
"FileNameColumn": "USR_AvatarFileName",
"FileSizeColumn": "USR_AvatarFileSize",
"FileMimeTypeColumn": "USR_AvatarMimeType",
"MaxFileSizeInKB" : 128,
"IsNullable" : True,
"AllowedMimeTypes" : allowedMimeTypes,
"AllowedExtensions" : allowedExtensions,
"ContentProcessing": ContentProcessing.Compress,
"StorageType": StorageType.BlobStore
})
contact.Properties.Add(fileProperty)
Context.SaveModel(model)
Using the Property
You can use the FileProperty just like you use any other property type. You can add a BoundCell to your list and form and time cockpit will automatically discover that your property represents a file and will display it appropriately (e.g. if it is an image it will display the image in the list). If you want more control over how the file property is displayed in your list or form use FileCell instead of BoundCell.
The following example uses a BoundCell to display the file property USR_Avatar
.
<List AllowDelete="True" AllowEdit="True" Query="From I In USR_Contact.Include(*) Select I"
xmlns="clr-namespace:TimeCockpit.Data.DataModel.View;assembly=TimeCockpit.Data">
<BoundCell Content="=Current.USR_Avatar" />
<BoundCell Content="=Current.USR_FirstName" />
<BoundCell Content="=Current.USR_LastName" />
</List>