Skip to content

Instantly share code, notes, and snippets.

Created January 10, 2014 23:48
Show Gist options
  • Save anonymous/0f1442c986333f1899ee to your computer and use it in GitHub Desktop.
Save anonymous/0f1442c986333f1899ee to your computer and use it in GitHub Desktop.
using EPiServer.Web;
public class DocumentFileHandler : StaticFileHandler
{
public override IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, object state)
{
// Force Office-application to not open document directly from URL
var extension = Path.GetExtension(context.Request.Path);
if (OfficeExtensions.Contains(extension))
context.Response.AppendHeader("content-disposition", "attachment");
return base.BeginProcessRequest(context, callback, state);
}
protected override bool ProcessRequestInternal(HttpContext context)
{
if (base.ProcessRequestInternal(context))
{
var extension = Path.GetExtension(context.Request.Path); // override missmatch in EPiServer
if (extension == ".pptx")
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
var virtualFile = GenericHostingEnvironment.VirtualPathProvider.GetFile(context.Request.FilePath);
// TODO: Do something with the file...we add stats for number of downloads and handle some security stuff in here....
}
return false;
}
private static IEnumerable<string> OfficeExtensions
{
get
{
return new[] { ".doc", ".ppt", ".xls", ".docx", ".pptx", ".xlsx" };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment