0

We're using ASP.NET MVC website with Kentico 11. When a file is uploaded into the CMS using CMS.File page type, we need to retrieve it on MVC side.

I can do the following maybe?

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

Assuming the API found the file, how to I get access to the binary data of the file so that I can return it to the browser?

3 Answers 3

1

Even if you are using file page type - behind the scene you are still working with attachments. You should look at attachment api and AttachmentInfoProvider class

so if you have page object you can do something like

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

or

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

Not sure which one is more applicable, but it should give you the idea...

P.S. you may also take a look kentico MVC project on github and search for attachment

P.P.S. Check as well Working with page attachments in MVC applications

Sign up to request clarification or add additional context in comments.

5 Comments

Got the DocumentAttachment object using AllAttachments property. But the AttachmentBinary property which I'm assuming contains the byte array of the file is null. Any ideas?
check the last link Working with page attachments in MVC applications There is a topic Creating page attachment URLs
Yes I checked that already. I could not find anything in that article explaining how to work with built-in CMS.DocumentEngine.Types.CMS.File
if you have attachment GUID you can for url to your attachment like ~/getattachment/<attachment GUID>/<filename>
The Kentico CMS returns 404 on that URL. The GUID is correct as well as filename. Actually, attachment is the file itself. Are you sure this is the correct way?
0

Thanks so much for the pointers. I was able to get the attachment and return to browser using following approach. The key was using GUID of attachment, but name of the document.

Code needs some cleanup but just sharing in case someone needs it:

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();
}

Comments

0

I did something like this for images, so I modified mine to hopefully work in your scenario. The thing to note is that the AttachmentBinary isn't returned unless you call the overload and pass in true to return it.

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.