如何设置fckeditor的上传文件的大小

用了FCKeditor以后才知道,在性能上确实是挺优越的,特别是在加载的速度上,远比其它的编辑器要来得快,而且跨语言跨平台,也不会像FreeTextBox那样在页面中加入一大堆的ViewState视图状态代码,减轻了页面文件的重量,提高了加载速度.

编辑器本身也内置了文件上传功能,但他却不对文件的类型以及大小做出限制,以至于带有安全忧患,万一给人上传了一个木马或者一个上面兆的影片文件怎么办,当然,修改*config.js文件可以解决,但似乎存在着某方面的安全隐患吧.

由于FCKeditor本身是开源的,所以我可以对里面的某些代码进行修改.

首先是对FileWorkerBase.cs基类的修改

using System;

namespace FredCK.FCKeditorV2
...{
    public abstract class FileWorkerBase : System.Web.UI.Page
    ...{
        private const string DEFAULT_USER_FILES_PATH = "/UserFiles/";

        private const string DEFAULT_USER_FILES_UPLOADTYPE = ".jpg.jpeg.bmp.gif.png.zip.rar.swf.";//默认允许上传文件类型
        private const int DEFAULT_USER_FILES_UPLOADSIZE = 1024;//默认允许上传文件大小(1024KB)

        private string sUserFilesPath;
        private string sUserFilesDirectory;

        private string sUserUploadType;
        private int iUserUploadSize = 0;

        protected string UserFilesPath
        ...{
            get
            ...{
                if (sUserFilesPath == null)
                ...{
                    // Try to get from the "Application".
                    sUserFilesPath = (string)Application["FCKeditor:UserFilesPath"];

                    // Try to get from the "Session".
                    if (sUserFilesPath == null || sUserFilesPath.Length == 0)
                    ...{
                        sUserFilesPath = (string)Session["FCKeditor:UserFilesPath"];

                        // Try to get from the Web.config file.
                        if (sUserFilesPath == null || sUserFilesPath.Length == 0)
                        ...{

                            sUserFilesPath = System.Web.Configuration.WebConfigurationManager.AppSettings["FCKeditor:UserFilesPath"];

                            // Otherwise use the default value.
                            if (sUserFilesPath == null || sUserFilesPath.Length == 0)
                                sUserFilesPath = DEFAULT_USER_FILES_PATH;

                            // Try to get from the URL.
                            if (sUserFilesPath == null || sUserFilesPath.Length == 0)
                            ...{
                                sUserFilesPath = Request.QueryString["ServerPath"];
                            }
                        }
                    }

                    // Check that the user path ends with slash ("/")
                    if (!sUserFilesPath.EndsWith("/"))
                        sUserFilesPath += "/";
                }
                return sUserFilesPath;
            }
        }


        /** <summary>
        /// The absolution path (server side) of the user files directory. It
        /// is based on the <see cref="FileWorkerBase.UserFilesPath"/>.
        /// </summary>
        protected string UserFilesDirectory
        ...{
            get
            ...{
                if (sUserFilesDirectory == null)
                ...{
                    // Get the local (server) directory path translation.
                    sUserFilesDirectory = Server.MapPath(this.UserFilesPath);
                }
                return sUserFilesDirectory;
            }
        }

        /** <summary>
        /// 获取允许上传的类型
        /// </summary>
        protected string UserUploadType
        ...{
            get
            ...{
                if (sUserUploadType == null)
                ...{
                    // Try to get from the "Application".
                    sUserUploadType = (string)Application["FCKeditor:UserUploadType"];

                    // Try to get from the "Session".
                    if (sUserUploadType == null || sUserUploadType.Length == 0)
                    ...{
                        sUserUploadType = (string)Session["FCKeditor:UserUploadType"];

                        // Try to get from the Web.config file.
                        if (sUserUploadType == null || sUserUploadType.Length == 0)
                        ...{

                            sUserUploadType = System.Web.Configuration.WebConfigurationManager.AppSettings["FCKeditor:UserUploadType"];

                            // Otherwise use the default value.
                            if (sUserUploadType == null || sUserUploadType.Length == 0)
                                sUserUploadType = DEFAULT_USER_FILES_UPLOADTYPE;

                        }
                    }

                    // Check that the user path starts and ends with slash (".")
                    if (!sUserUploadType.StartsWith("."))
                        sUserUploadType = "." + sUserUploadType;

                    if (!sUserUploadType.EndsWith("."))
                        sUserUploadType += ".";
                }
                return sUserUploadType;
            }
        }

        /** <summary>
        /// 获取允许上传的文件最大限制
        /// </summary>
        protected int UserUploadSize
        ...{
            get
            ...{
                if (iUserUploadSize < 1)
                ...{
                    iUserUploadSize = Convert.ToInt32(Application["FCKeditor:UserUploadSize"]);
                    if (iUserUploadSize < 1)
                    ...{
                        iUserUploadSize = Convert.ToInt32(Session["FCKeditor:UserUploadSize"]);
                        if (iUserUploadSize < 1)
                        ...{
                            iUserUploadSize = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["FCKeditor:UserUploadSize"]);
                            if (iUserUploadSize < 1)
                            ...{
                                iUserUploadSize = DEFAULT_USER_FILES_UPLOADSIZE;
                            }
                        }
                    }
                }

                return iUserUploadSize;
            }
        }
    }
}

接着就是对点击"浏览服务器"页面的上传部分的修改

以下是对FileBrowserConnector.cs中的FileUpload()函数的修改

  private void FileUpload(string resourceType, string currentFolder)
        ...{
            HttpPostedFile oFile = Request.Files["NewFile"];

            string sErrorNumber = "0";
            string sFileName = "";

            if (oFile != null && oFile.ContentLength > 0)
            ...{
                // Map the virtual path to the local server path.
                string sServerDir = this.ServerMapFolder(resourceType, currentFolder);
                /**//*
                // Get the uploaded file name.
                sFileName = System.IO.Path.GetFileName( oFile.FileName ) ;

                int iCounter = 0 ;

                while ( true )
                {
                    string sFilePath = System.IO.Path.Combine( sServerDir, sFileName ) ;

                    if ( System.IO.File.Exists( sFilePath ) )
                    {
                        iCounter++ ;
                        sFileName =
                            System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
                            "(" + iCounter + ")" +
                            System.IO.Path.GetExtension( oFile.FileName ) ;

                        sErrorNumber = "201" ;
                    }
                    else
                    {
                        oFile.SaveAs( sFilePath ) ;
                        break ;
                    }
                }
                */
                if (this.UserUploadType.ToLower().IndexOf(System.IO.Path.GetExtension(oFile.FileName).ToLower() + ".") > -1)//检测是否为允许的上传文件类型
                ...{
                    if (this.UserUploadSize * 1024 >= oFile.ContentLength)//检测文件大小是否超过限制
                    ...{
                        sFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(oFile.FileName);
                        string sFilePath = System.IO.Path.Combine(sServerDir, sFileName);
                        oFile.SaveAs(sFilePath);
                    }
                    else//文件大小超过限制
                    ...{
                        Response.Clear();

                        Response.Write("<script type="text/javascript">");
                        Response.Write("window.parent.frames['frmUpload'].OnUploadCompleted(1,'上传文件大小超出限制') ;");
                        Response.Write("</script>");

                        Response.End();
                    }
                }
                else //文件类型不允许上传
                ...{
                    Response.Clear();

                    Response.Write("<script type="text/javascript">");
                    Response.Write("window.parent.frames['frmUpload'].OnUploadCompleted(1,'上传文件类型不允许') ;");
                    Response.Write("</script>");

                    Response.End();
                }

 

            }
            else
                sErrorNumber = "202";

            Response.Clear();

            Response.Write("<script type="text/javascript">");
            Response.Write("window.parent.frames['frmUpload'].OnUploadCompleted(" + sErrorNumber + ",'" + sFileName.Replace("'", "/'") + "') ;");
            Response.Write("</script>");

            Response.End();
        }
最后就是对Uploader.cs类中的OnLoad()函数的修改

 protected override void OnLoad(EventArgs e)
        ...{
            // Get the posted file.
            HttpPostedFile oFile = Request.Files["NewFile"];

            // Check if the file has been correctly uploaded
            if (oFile == null || oFile.ContentLength == 0)
            ...{
                SendResults(202);
                return;
            }

            int iErrorNumber = 0;
            string sFileUrl = "";
            string sFileName = "";
            //使用原文件名上传代码,如果文件名相同,则在后面加上标号(1)(2)...
            /**//*
            // Get the uploaded file name.
            string sFileName = System.IO.Path.GetFileName( oFile.FileName ) ;

           
            int iCounter = 0 ;

            while ( true )
            {
                string sFilePath = System.IO.Path.Combine( this.UserFilesDirectory, sFileName ) ;

                if ( System.IO.File.Exists( sFilePath ) )
                {
                    iCounter++ ;
                    sFileName =
                        System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
                        "(" + iCounter + ")" +
                        System.IO.Path.GetExtension( oFile.FileName ) ;

                    iErrorNumber = 201 ;
                }
                else
                {
                    oFile.SaveAs( sFilePath ) ;

                    sFileUrl = this.UserFilesPath + sFileName ;
                    break ;
                }
            }
             */
            //使用原文件名上传代码结束
            //使用时间作为流水号文件名


            if (this.UserUploadSize * 1024 >= oFile.ContentLength)//检测文件大小是否超过限制
            ...{
                sFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(oFile.FileName);
                string sFilePath = System.IO.Path.Combine(this.UserFilesDirectory, sFileName);
                oFile.SaveAs(sFilePath);

                sFileUrl = this.UserFilesPath + sFileName;
            }
            else//文件大小超过限制
            ...{
                SendResults(1, "", "", "上传文件大小超出限制");

            }

            /**//

            SendResults(iErrorNumber, sFileUrl, sFileName);
        }
最后只要在Web.Config文件中加入对文件上传的限制值就可以了.

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings> 
<add key="FCKeditor:UserFilesPath" value="/UserFiles/" />
<add key="FCKeditor:UserUploadType" value=".gif.jpg.jpeg.rar.zip.swf.png" />
<add key="FCKeditor:UserUploadSize" value="5120" /><!--单位为KB--> 
 </appSettings>
 <system.web>
     <httpRuntime maxRequestLength="512000" /> 
  </system.web>

 
</configuration>

最近做了一个文章管理的系统,用到在线编辑器,修改了一下现在能实现音频,视频,图片,附件,Flash等文件的上传。费了好大的功夫。。 FCKeditor应用在ASP.NET上,需要两组文件,一组是FCKeditor本身,另一个是用于ASP.NET的FCKeditor控件(分为1.1和2.0两个版本,这里使用2.0版本)。 1. 将FCKeditor加入到项目中 解压FCKeditor编辑器,得到文件夹fckeditor,复制此文件夹到Web应用的项目下(也可以是子孙目录下)。 解压FCKeditor控件,在其子目录bin/Release/2.0下有一个程序集。在Web应用的项目中引用该程序集。 2. 在页面中使用FCKeditor 有两种方式。 (1)手工编码 在页面中加入ASP.NET指令: 然后在需要的地方加入FCKeditor控件: (2)集成到Visual Studio工具箱 打开一ASP.NET页面,展开Toolbox,打开右键菜单,选择“Choose Items ...”,在出现的“Choose Toolbox Items”会话框的“.NET Framework Components”选项卡中选择“Browse”,找到并选中FCKeditor程序集,打开后回到“Choose Toolbox Items”窗口,点击“OK”,完成控件导入。 这时,在Toolbox的General分类下出现了一个名为FCKeditor的控件,可以像使用Visual Studio内置控件一样使用它。 3. 配置FCKeditor编辑器路径 在页面中,使用的是FCKeditor控件,该控件需要知道FCKeditor编辑器文件组的路径。有两种配置方法。 (1)配置web.config 在appSettings配置节中加入 使用这种配置方法后,对于项目中任何一个页面中用到的FCKeditor控件,都不用再配置其BasePath属性。 (2)直接对用到的FCKeditor控件进行配置 在页面代码中设置FCKeditor的属性BasePath为FCKeditor编辑器文件组的路径,或者在Page_Init事件处理器中设置其BasePath的值。 4. 配置FCKeditor编辑器文件上传路径 在web.config的appSettings配置节中加入 或者 这样,就完成了FCKeditor向ASP.NET页面的集成工作。 二、配置FCKeditor 按照FCKeditor的默认配置,可以完成一些常用的HTML可视化编辑工作,但在实际应用中,还需要对其做进一步的配置。FCKeditor控件的可配置属性不多,且配置后只能作用于一个单一实例。实际上,需要对FCKeditor编辑器文件组中的通用配置文件/fckconfig.js和ASP.NET专用文件上传管理代码文件/editor/filemanager/connectors/aspx/config.ascx进行配置。 1. 配置控件语言 FCKeditor是自动探测浏览器所使用的语言编码的,其默认语言是英文。修改配置行"FCKConfig.DefaultLanguage = 'en';"为'zh-cn',采用中文为默认语言。 2. 配置控件应用技术 FCKeditor默认是用于php技术的。修改配置行"var _FileBrowserLanguage = 'php';"和"var _
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路奔跑1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值