从 CMS.File 页面类型中检索二进制数据
Retrieve binary data from CMS.File page type
我们正在使用 ASP.NET MVC 网站和 Kentico 11。当使用 CMS.File 页面类型将文件上传到 CMS 时,我们需要在 MVC 端检索它。
我可以做以下事情吗?
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
假设 API 找到了文件,如何访问文件的二进制数据以便我可以 return 将其发送到浏览器?
即使您使用的是文件页面类型 - 在后台您仍在处理附件。你应该看看 attachment api and AttachmentInfoProvider class
所以如果你有页面对象,你可以做类似
的事情
DocumentAttachment da = page?.AllAttachments.FirstOrDefault();
或
var attachment = AttachmentInfoProvider.GetAttachments()
.WhereEquals("ColumnFromCMS_Attachment", "value")
.FirstOrDefault();
不确定哪个更适用,但它应该能给你思路...
P.S。你也可以看看kentico MVC project on github and search for attachment
多谢指点。我能够使用以下方法将附件和 return 获取到浏览器。关键是使用附件的 GUID,但使用文档的名称。
代码需要一些清理,但只是分享以防有人需要它:
public ActionResult FilePage(string completeAlias)
{
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
if (kntcoFile != null)
{
DocumentAttachment attachment = kntcoFile.AllAttachments.FirstOrDefault();
if (attachment != null)
{
string kenticoSite = System.Configuration.ConfigurationManager.AppSettings["KenticoSite"];
string fileUrl = string.Format("{0}getattachment/{1}/{2}", kenticoSite, attachment.AttachmentGUID, kntcoFile.DocumentName);
byte[] fileBytes = null;
using (WebClient wc = new WebClient())
{
fileBytes = wc.DownloadData(fileUrl);
}
return new FileContentResult(fileBytes, attachment.AttachmentMimeType);
}
}
return new HttpNotFoundResult();
}
我对图像做了类似的事情,所以我修改了我的图像以希望在您的场景中工作。需要注意的是,除非您调用重载并将 true 传递给 return,否则不会 return 编辑 AttachmentBinary。
public ActionResult FilePage(string completeAlias)
{
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
if (kntcoFile != null)
{
DocumentAttachment attachment = kntcoFile.AllAttachments.FirstOrDefault();
if (attachment != null)
{
var attachmentBinary = AttachmentInfoProvider.GetAttachmentInfo(attachment.AttachmentID, true);
return base.File(attachmentBinary.AttachmentBinary, attachment.AttachmentMimeType);
}
}
EventLogProvider.LogInformation("GetFile", "NOTFOUND", "attachment Not Found" + completeAlias + " /");
return null;
}
我们正在使用 ASP.NET MVC 网站和 Kentico 11。当使用 CMS.File 页面类型将文件上传到 CMS 时,我们需要在 MVC 端检索它。
我可以做以下事情吗?
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
假设 API 找到了文件,如何访问文件的二进制数据以便我可以 return 将其发送到浏览器?
即使您使用的是文件页面类型 - 在后台您仍在处理附件。你应该看看 attachment api and AttachmentInfoProvider class
所以如果你有页面对象,你可以做类似
的事情DocumentAttachment da = page?.AllAttachments.FirstOrDefault();
或
var attachment = AttachmentInfoProvider.GetAttachments()
.WhereEquals("ColumnFromCMS_Attachment", "value")
.FirstOrDefault();
不确定哪个更适用,但它应该能给你思路...
P.S。你也可以看看kentico MVC project on github and search for attachment
多谢指点。我能够使用以下方法将附件和 return 获取到浏览器。关键是使用附件的 GUID,但使用文档的名称。
代码需要一些清理,但只是分享以防有人需要它:
public ActionResult FilePage(string completeAlias)
{
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
if (kntcoFile != null)
{
DocumentAttachment attachment = kntcoFile.AllAttachments.FirstOrDefault();
if (attachment != null)
{
string kenticoSite = System.Configuration.ConfigurationManager.AppSettings["KenticoSite"];
string fileUrl = string.Format("{0}getattachment/{1}/{2}", kenticoSite, attachment.AttachmentGUID, kntcoFile.DocumentName);
byte[] fileBytes = null;
using (WebClient wc = new WebClient())
{
fileBytes = wc.DownloadData(fileUrl);
}
return new FileContentResult(fileBytes, attachment.AttachmentMimeType);
}
}
return new HttpNotFoundResult();
}
我对图像做了类似的事情,所以我修改了我的图像以希望在您的场景中工作。需要注意的是,除非您调用重载并将 true 传递给 return,否则不会 return 编辑 AttachmentBinary。
public ActionResult FilePage(string completeAlias)
{
var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();
if (kntcoFile != null)
{
DocumentAttachment attachment = kntcoFile.AllAttachments.FirstOrDefault();
if (attachment != null)
{
var attachmentBinary = AttachmentInfoProvider.GetAttachmentInfo(attachment.AttachmentID, true);
return base.File(attachmentBinary.AttachmentBinary, attachment.AttachmentMimeType);
}
}
EventLogProvider.LogInformation("GetFile", "NOTFOUND", "attachment Not Found" + completeAlias + " /");
return null;
}