0
votes

Working with Kentico 11.0.26 CMS and a MVC website.

Have a custom content-only page type with an image field. After the image is uploaded on a page I need to display it on MVC site. But Kentico's generated code MyPageTypeProvider.GetMyPageType((int nodeId, string cultureName, string siteName) returns a page object that only contains the GUID of the image. No bytes, no URL.

How do I get the bytes or the URL of the uploaded image?

2

2 Answers

0
votes

If you need the bytes, you can do this:

var attachment =DocumentHelper.GetAttachment(guid, SiteContext.CurrentSiteName, true);
var bytes = attachment.AttachmentBinary;

If you want a URL to the image, you can do something like this:

 imageUrl = $"/getattachment/{guid}/attachment.aspx"

This documentation explains more ways to work with attachments.

0
votes

You will need to either resolve the URL, or get file by GUID. Problem is, that Kentico Nuget API does not seem to provide enough options to get file binaries.

HelperMethods from Kentico.Content.Web.MVC NuGet seem to be good start:

https://github.com/Kentico/Mvc/tree/master/src/Kentico.Content.Web.Mvc

With these you can get file URL and use:

using (var client = new WebClient())
{
    client.DownloadFile("http://example.com/file/song/a.mpeg", "a.mpeg");
}

Or you can write your own class or service, reference Kentico DLLs and use:

AttachmentBinaryHelper.GetFilePhysicalPath(string siteName, string guid, string extension)