【问题标题】:Retrieve binary data from CMS.File page type从 CMS.File 页面类型中检索二进制数据
【发布时间】:2019-01-11 02:57:22
【问题描述】:

我们在 Kentico 11 中使用 ASP.NET MVC 网站。当使用 CMS.File 页面类型将文件上传到 CMS 时,我们需要在 MVC 端检索它。

也许我可以做到以下几点?

var kntcoFile = FileProvider.GetFile(completeAlias, "en-US", "MySite").FirstOrDefault();

假设 API 找到了文件,我如何访问文件的二进制数据,以便将其返回给浏览器?

【问题讨论】:

    标签: asp.net-mvc kentico


    【解决方案1】:

    即使您使用的是文件页面类型 - 在幕后您仍在处理附件。你应该看看attachment apiAttachmentInfoProvider

    所以如果你有页面对象,你可以做类似的事情

    DocumentAttachment da = page?.AllAttachments.FirstOrDefault();
    

       var attachment = AttachmentInfoProvider.GetAttachments()
           .WhereEquals("ColumnFromCMS_Attachment", "value")
           .FirstOrDefault();
    

    不确定哪个更适用,但它应该给你的想法......

    附:你也可以看看kentico MVC project on github and search for attachment

    附言检查以及Working with page attachments in MVC applications

    【讨论】:

    • 使用AllAttachments 属性获得了DocumentAttachment 对象。但是我假设包含文件的字节数组的AttachmentBinary 属性为空。有什么想法吗?
    • 查看最后一个链接Working with page attachments in MVC applications有一个主题创建页面附件URL
    • 是的,我已经检查过了。我在那篇文章中找不到任何解释如何使用内置 CMS.DocumentEngine.Types.CMS.File
    • 如果您有附件 GUID,您可以为附件提供 URL,例如 ~/getattachment/<attachment GUID>/<filename>
    • Kentico CMS 在该 URL 上返回 404。 GUID 和文件名都是正确的。实际上,附件就是文件本身。你确定这是正确的方法吗?
    【解决方案2】:

    非常感谢您的指点。我能够使用以下方法获取附件并返回浏览器。关键是使用附件的 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();
    }
    

    【讨论】:

      【解决方案3】:

      我为图像做了类似的事情,所以我修改了我的,希望能在你的场景中工作。需要注意的是,除非您调用重载并传入 true 以返回它,否则不会返回 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 1970-01-01
        • 2011-06-30
        • 2021-08-02
        • 1970-01-01
        相关资源
        最近更新 更多