通过HttpModule实现Fckeditor的分目录上传

标题不知该怎么写更能表达意思,近期遇到一个中英文版的网站项目,中文和英文的后台都是用的一个Fckeditor,很自然的想到中文和英文通过编辑器上传的文件应该分开存放到各自语言版本的目录中,如各自目录下的UploadFile文件夹,通过网上查资料得知,可以通过设置Session["FCKeditor:UserFilesPath"]来动态改变Fckeditor的上传路径,但这样会带来一个比较烦索的事情就是中文文件的admin下(假定网站根目录为英文,中文位于根目录的/cn/下,每个语言版本的管理后台位于各自的Admin目录下)的文件中,凡是用到Fckeditor的地方都要设置一遍Session["FCKeditor:UserFilesPath"],由此想到是否可以通过HttpModule来实现根据网站目录自动设置Session["FCKeditor:UserFilesPath"],经过测试,整理代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace FckConfig
{
/// <summary>
/// FckUploadConfig 的摘要说明
/// </summary>
public class FckUploadConfig : IHttpModule

{
private void Application_AcquireRequestState(object sender, EventArgs e)
{

HttpApplication application = (HttpApplication)sender;

HttpContext context = application.Context;

HttpRequest request = application.Request;

HttpResponse response = application.Response;

if (request.Path.ToLower().IndexOf("/cn/admin") == 0)
{
context.Session["FCKeditor:UserFilesPath"] = "~/cn/UploadFile/";
}
if (request.Path.ToLower().IndexOf("/admin") == 0)
{
context.Session["FCKeditor:UserFilesPath"] = "~/UploadFile/";
}
}

public void Dispose()

{ }

public void Init(HttpApplication application)
{
application.AcquireRequestState +=new EventHandler(Application_AcquireRequestState);
}
}
}

需要在Web.Config中设置一下:

    <httpModules>
<add name="test" type="FckConfig.FckUploadConfig"/>
</httpModules>

这样,就可以实现按目录自动设置Session["FCKeditor:UserFilesPath"]了。

原文地址:https://www.cnblogs.com/superfeeling/p/2322600.html