Get FileInfo from a file in a TreeNode

Paul Cripps asked on October 22, 2021 12:44

I am trying to access the FileInfo of a file stored in a TreeNode. It looks like I should be able to use the GetFileInfo method of the TreeNode but I don't know what the 'columnName' property of the method should be.

TreeNode node = DocumentHelper.GetDocument("ACNS", fileName, CMS.Localization.LocalizationContext.PreferredCultureCode, true, "CMS.File", null, null, -3, false, null, new TreeProvider());
if (node != null)
{
    if (node.IsFile()){
        CMS.IO.FileInfo fileInfo = node.GetFileInfo("*columnName*");
        if (fileInfo != null)
        {
            //Extract the information from fileInfo
        }
    }
}

Recent Answers


David te Kloese answered on October 29, 2021 15:05

I believe that method is used if you store the file on the node in a specific column. Say you have a page type "article" with a file stored in the field "ArticlePDFVersion" you would supply the field as a column.

How are the files stored? You say in the treenode that's likely to be an attachment? or did you use the out of the box page type "CMS.File" (which has a field 'FileAttachment')

0 votesVote for this answer Mark as a Correct answer

Paul Cripps answered on October 29, 2021 15:25

The files have been directly uploaded to the tree. I clicked on a node in the tree and Selected New|File and then uploaded the file.

I have created a page web part that provides a link to this file so that it can be downloaded. I am looking to change the WebPart so that it uses a IHttpHandler instead so that before the file is downloaded a record the download can be stored. Once the download is recorded the Handler will extract the FileInfo so that it can download the file.

Below is the code I have so far so hopefully you can see what I am trying to do.

<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.Web;

using System.IO;
using CMS.Membership;
using CMS.DocumentEngine;
using CMS.Helpers;

public class DownloadHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string fileName = context.Request.QueryString["filename"].ToString();
        FileInfo file = new System.IO.FileInfo(fileName);
        TreeNode node = DocumentHelper.GetDocument("ACNS", fileName, CMS.Localization.LocalizationContext.PreferredCultureCode, true, "CMS.File", null, null, -3, false, null, new TreeProvider());
        if (node != null)
        {
            if (node.IsFile()){

               CMS.IO.FileInfo fileInfo = node.GetFileInfo("XXXX");

                if (fileInfo != null)
                {
                    string contentType = "text/plain";
                    string username = "anonymous";

                    try
                    {
                        switch (fileInfo.Extension.ToLower())  // .EndsWith("mp3", StringComparison.OrdinalIgnoreCase))
                        {
                            case "pdf":
                                contentType = "application/pdf";
                                break;
                            case "doc":
                            case "docx":
                                contentType = "application/msword";
                                break;
                            case "xls":
                            case "xlsx":
                                contentType = "application/msexcel";
                                break;
                        }

                        UserInfo userInfo = MembershipContext.AuthenticatedUser;
                        if (userInfo != null && AuthenticationHelper.IsAuthenticated())
                        {
                            username = userInfo.UserName;
                        }
                        //Log the download

                    }
                    catch (Exception)
                    {
                        //handle the situation gracefully.
                    }
                    //return the file

                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                    context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    context.Response.ContentType = contentType;
                    context.Response.WriteFile(fileInfo.FullName);
                    context.ApplicationInstance.CompleteRequest();
                    context.Response.End();
                }
            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.