你好,游客 登录 注册 搜索
背景:
阅读新闻

在Windows Azure平台上管理文件

[日期:2012-07-27] 来源:Linux社区  作者:aa466564931 [字体:大 中 小]

接着我们要在WebRole中创建一个HttpModuler来检测用户的访问和并且将禁止的request扔到错误处理的页面中去:

FileManagerModuler.cs

  1. public void Init(HttpApplication context)  
  2. {             
  3.     context.BeginRequest += new EventHandler(this.Application_BeginRequest);  
  4. }  
  5.   
  6. /// <summary>   
  7. /// Check file types and request it by cloud blob API.   
  8. /// </summary>   
  9. /// <param name="source"></param>   
  10. /// <param name="e"></param>   
  11. private void Application_BeginRequest(Object source,EventArgs e)  
  12. {  
  13.     string url = HttpContext.Current.Request.Url.ToString();  
  14.     string fileName = url.Substring(url.LastIndexOf('/') + 1).ToString();  
  15.     string extensionName = string.Empty;  
  16.     if (fileName.Substring(fileName.LastIndexOf('.') + 1).ToString().Equals("aspx"))  
  17.     {  
  18.         return;  
  19.     }  
  20.   
  21.     if (!fileName.Equals(string.Empty))  
  22.     {  
  23.         extensionName = fileName.Substring(fileName.LastIndexOf('.') + 1).ToString();  
  24.         if (!extensionName.Equals(string.Empty))  
  25.         {  
  26.             string contentType = this.GetContentType(fileName);  
  27.             if (contentType.Equals(string.Empty))  
  28.             {  
  29.                 HttpContext.Current.Server.Transfer("NoHandler.aspx");  
  30.             };  
  31.             {  
  32.                 FileDataSource dataSource = new FileDataSource();  
  33.                 string paritionKey = this.GetPartitionName(extensionName);  
  34.                 if (String.IsNullOrEmpty(paritionKey))  
  35.                 {  
  36.                     HttpContext.Current.Server.Transfer("NoHandler.aspx");  
  37.                 }  
  38.                 FileEntity entity = dataSource.GetEntitiesByName(paritionKey, "Files/" + fileName);  
  39.                 if (entity != null)  
  40.                     HttpContext.Current.Response.Redirect(entity.FileUrl);  
  41.                 else  
  42.                     HttpContext.Current.Server.Transfer("NoResources.aspx");  
  43.             }  
  44.         }  
  45.     }  
  46. }  
  47.   
  48. /// <summary>   
  49. /// Get file's Content-Type.   
  50. /// </summary>   
  51. /// <param name="filename"></param>   
  52. /// <returns></returns>   
  53. public string GetContentType(string filename)  
  54. {  
  55.     string res = string.Empty;  
  56.   
  57.     switch (filename.Substring(filename.LastIndexOf('.') + 1).ToString().ToLower())  
  58.     {  
  59.         case "jpg":  
  60.             res = "image/jpg";  
  61.             break;  
  62.         case "css":  
  63.             res = "text/css";  
  64.             break;  
  65.     }  
  66.   
  67.     return res;  
  68. }  
  69.   
  70. /// <summary>   
  71. /// Get file's partitionKey.   
  72. /// </summary>   
  73. /// <param name="extensionName"></param>   
  74. /// <returns></returns>   
  75. public string GetPartitionName(string extensionName)  
  76. {  
  77.     string partitionName = string.Empty;  
  78.     switch(extensionName)  
  79.     {  
  80.         case "jpg":  
  81.             partitionName = "image";  
  82.             break;  
  83.         case "css":  
  84.             partitionName = "css";  
  85.             break;  
  86.     }  
  87.     return partitionName;  
  88. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款