@Attachment (jello.annotation.Attachment)
Mark the field as an Attachment field which will refer to a document stored in the Blobstore. The field value will hold the blob id and must be of a type String
. You can specify the file type (optional) and it will be enforced by the server.
@Expose @Attachment String attachmentField; @Expose @Attachment(accept="image/png") String photo1; @Expose @Attachment(accept="image/*") String photo2;Jello manages the entire lifecycle of the attachment as follow:
- Get upload URL
Before you can upload an attachment, you need to obtain a valid upload URL. This URL is associated with a specific element field on a JEllo entity and will remain valid for a short period of time, typically less then 10 minutes.
Getting upload URL for an attachment field on a new instance:
/jello/upload/NS/Entity/element
Getting upload URL for an attachment field on ane existing instance:
/jello/upload/NS/Entity(id)/element
Response:
{ "d" : {"uploadUrl" : "the-upload-URL"}}
- Uploading an attachment
To creat a custom upload form, the form must include a file upload field, and the form's enctype
must be set to multipart/form-data
.
You can use the field metadata to specify the ecpected field's type:
{ ... fields: [ { name: "photo", type: "jello.Attachment(image/*)" }, ... ] }
refer to the Entity Metadata chapter to read more about the metadata response format)
In addition, Jello Form provides a special attachment control for attachment fields with built-in file upload capabiliy:
Upon upload complete, the attachment is temporarily kept in the BlobStore and the response will contain the following details:
d { fileName: "Angelina Jolie.jpg" fileType: "image/jpeg" key: "ICpxhCE2TJnSnATv_Q4P6Q" size: 14426 }
The actual value that need to be saved in the attachment field is the key
. The attachment will be deleted if you will not save the associated instance in a timely manner (typically within 10 minutes).
- Download / Display
To get the actual value of the attachment, use the following REST call:
/jello/data/NS/Entity(id)/element$value
If the attachment is an image, this call will return the actual image binary content (much like referring to any image URL).
For any other file types, this call will download the file. To download an image attachment, add the following to the request:
/jello/data/NS/Entity(id)/element$value?$download=true
Sometime it can be useful to display the uploaded image even if it was not yet been saved. It typically done in order to give the user feedback before saving. To get the uploaded image use the following special request:
/jello/data/NS/Entity(id)/element<attachment-key>$value
- Get the attachment Info
/jello/data/NS/Entity(id)/element$info
You can also get the attachment info by using $expand:
$attachmentPreview=fileName|fileType|fileSize|image|key (default=image)
Was this page helpful? Let us know how we did:
Last updated December 30, 2015.