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

时间:2018-11-26 02:32:25

标签: asp.net-mvc kentico

使用Kentico 11.0.26 CMS和MVC网站。

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

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

2 个答案:

答案 0 :(得分:0)

您将需要解析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");
}

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

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

答案 1 :(得分:0)

如果需要字节,则可以执行以下操作:

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

如果您想要图像的URL,可以执行以下操作:

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

This documentation介绍了使用附件的更多方法。