如何使用其 GUID 获取图像字节或 URL?

How to get image bytes or URL using its GUID?

使用 Kentico 11.0.26 CMS 和 MVC 网站。

具有带有图像字段的自定义纯内容页面类型。图片上传到页面后,我需要在 MVC 站点上显示它。但是 Kentico 生成的代码 MyPageTypeProvider.GetMyPageType((int nodeId, string cultureName, string siteName) returns 一个只包含图像 GUID 的页面对象。没有字节,没有 URL。

如何获取上传图片的字节数或URL?

您将需要解析 URL,或通过 GUID 获取文件。问题是,Kentico Nuget API 似乎没有提供足够的选项来获取文件二进制文件。

来自 Kentico.Content.Web.MVC NuGet 的 HelperMethods 似乎是一个好的开始:

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

通过这些你可以获得文件 URL 并使用:

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

或者您可以编写自己的 class 或服务,引用 Kentico DLL 并使用:

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

如果你需要字节,你可以这样做:

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

如果你想要 URL 图片,你可以这样做:

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

This documentation 解释了更多处理附件的方法。