Working with page attachments in MVC applications

Attachments represent data, for example images, stored in page type fields. Attachments are stored in the following data types of page type fields:

  • Attachments – allows you to store multiple attachments in a single page type field.
  • File – allows you to store a single page attachment in a page type field.

Kentico provides a standard API for working with attachments within the Kentico application. For working with attachments in MVC applications, Kentico provides a different API. The API is part of the code that you can generate for individual page types.

The API for working with attachments in MVC applications gives you more control than the standard API would when creating URLs for page attachments. You can create strongly typed views and the provided method for creating page attachment URLs is independent on Kentico’s settings. That is, the resulting URL is always in the same format and Kentico’s image resizing settings are not applied.

Example of working with page attachments using generatedpage type code




// Retrieves an article.
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite");

// Accesses a single attachment in the retrieved article's 'Teaser' field.
var teaserImageWidth = article.Fields.Teaser.ImageWidth;

...

// Iterates over attachments in the article's 'Images' field.
foreach (var image in article.Fields.Images)
{
    var imageMimeType = image.MimeType;
    ...
}


Creating page attachment URLs

  1. Perform the initial set up of your MVC application.

  2. Use the Url.Kentico().Attachment() method to retrieve page attachment URLs.

    
    
    
     <img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser)">
    
    
     
  3. The result is a relative URL in the following format:

    
    
    
     ~<domain>/getattachment/<file GUID>/<filename>
    
     
  4. See also:

Because the returned URLs are relative, the MVC site needs to run on the same domain as the site configured in Kentico. Another solution is setting up a domain alias leading to the MVC site.

Note: the system processes the file names and replaces all special characters with a ‘-’ (hyphen). All characters that are not 0-9, a-z (latin small letter a to latin small letter z), ‘.’ (full stop) or ‘_’ (underscore) are replaced.

Attachment versioning

The Url.Kentico().Attachment() method always returns the attachment that is attached to the version of the page you are currently working with. For example, using the Url.Kentico().Attachment() method on pages retrieved using the generated code providers always retrieves the attachment belonging to the published version of the page (the providers work with published versions of pages).

Constraining the returned attachment size for image attachments

You can further parametrize the Url.Kentico().Attachment method to retrieve attachments of specific size:

Specify the maximum height of the retrieved image. This constraint retains the original aspect ratio.




<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Height(200))">


Specify the maximum width of the retrieved image. This constraint retains the original aspect ratio.




<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Width(200))">


Specify both the maximum width and height of the retrieved image. This constraint does not retain the original aspect ratio.




<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Size(400, 1200))">


Specify the maximum width and height that the retrieved image cannot exceed. This constraint retains the original aspect ratio and the resized image is never made larger than the original.




<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.MaxWidthOrHeight(400))">


Prompting users for action when accessing attachments

You can specify whether the web browser will prompt users to perform an action, such as saving the file to a disk, when accessing attachments.




@using Kentico.Web.Mvc

@{
var urlOptions = new AttachmentUrlOptions { AttachmentContentDisposition = true }; 
}

<img alt="Article @article.Fields.Teaser" src="@Url.Kentico().Attachment(article.Fields.Teaser, SizeConstraint.Size(1280, 800), urlOptions))">