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