Created
June 21, 2012 09:49
-
-
Save anonymous/2964913 to your computer and use it in GitHub Desktop.
download handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Web; | |
using System.Web.UI; | |
using System.Xml.XPath; | |
namespace MyLovely.Web.App | |
{ | |
/// <summary> | |
/// Reads a media node's id from the querystring and writes it to the response. | |
/// </summary> | |
public partial class DownloadHandler : UserControl | |
{ | |
private string _queryStringKey = "mediaID"; | |
/// <summary> | |
/// The querystring parameter to read | |
/// </summary> | |
public string QueryStringKey | |
{ | |
get { return _queryStringKey; } | |
set | |
{ | |
if (!String.IsNullOrEmpty(value)) | |
{ | |
_queryStringKey = value; | |
} | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
string strMediaID = Request.QueryString[QueryStringKey]; | |
int mediaID; | |
if(Int32.TryParse(strMediaID, out mediaID)) | |
{ | |
WriteMediaFileToResponse(mediaID); | |
} | |
} | |
public void WriteMediaFileToResponse(int mediaID) | |
{ | |
XPathNavigator navigator = umbraco.library.GetMedia(mediaID, false).Current; | |
if (navigator != null) | |
{ | |
var filePathNode = navigator.SelectSingleNode("//umbracoFile"); | |
var fileNameNode = navigator.SelectSingleNode("//@nodeName"); | |
if (filePathNode != null && fileNameNode != null) | |
{ | |
string mediaFilePath = filePathNode.InnerXml; | |
string mediaFileName = HttpUtility.UrlPathEncode(fileNameNode.InnerXml); | |
string fileExtension = Path.GetExtension(mediaFilePath); | |
string attachment = String.Format("attachment; filename=\"{0}{1}\"", mediaFileName, fileExtension); | |
Response.Clear(); | |
Response.ClearHeaders(); | |
Response.ClearContent(); | |
Response.AddHeader("content-disposition", attachment); | |
Response.AddHeader("content-type", GetMimeType(fileExtension)); | |
Response.WriteFile(mediaFilePath); | |
} | |
} | |
} | |
private static string GetMimeType(string ext) | |
{ | |
if (String.IsNullOrEmpty(ext)) return "application/unknown"; | |
string mimeType; | |
switch (ext.ToLower()) | |
{ | |
case ".doc": | |
mimeType = "application/msword"; | |
break; | |
case ".docx": | |
mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; | |
break; | |
case ".eps": | |
mimeType = "application/postscript"; | |
break; | |
case ".exe": | |
mimeType = "application/octet-stream"; | |
break; | |
case ".flv": | |
mimeType = "video/x-flv"; | |
break; | |
case ".gif": | |
mimeType = "image/gif"; | |
break; | |
case ".jpeg": | |
case ".jpg": | |
mimeType = "image/jpeg"; | |
break; | |
case ".mp3": | |
mimeType = "audio/mpeg"; | |
break; | |
case ".pdf": | |
mimeType = "application/pdf"; | |
break; | |
case ".png": | |
mimeType = "image/png"; | |
break; | |
case ".ppt": | |
mimeType = "application/vnd.ms-powerpoint"; | |
break; | |
case ".pptx": | |
mimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; | |
break; | |
case ".tif": | |
case ".tiff": | |
mimeType = "image/tiff"; | |
break; | |
case ".txt": | |
mimeType = "text/plain"; | |
break; | |
case ".xls": | |
mimeType = "application/vnd.ms-excel"; | |
break; | |
case ".xlsx": | |
mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; | |
break; | |
default: | |
mimeType = "application/unknown"; | |
break; | |
} | |
return mimeType; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment