自感觉比CkEditor更顺手的KIndEditor

来源:互联网 发布:淘宝有些东西不能评价 编辑:程序博客网 时间:2024/05/18 02:48

首先下载KindEditor   http://www.kindsoft.net

下载后目录中存在以下文件夹:demo.aspx基本不用改动别的。主要是upload_json.ash文件和file_manager_json.ashx文件

upload_json.ashx文件用于异步上传图片,代码如下:

public void ProcessRequest(HttpContext context)
    {
        
        //文件保存目录路径
        String savePath = "G:/KindEditor-Up- Imag/KindEditor/Kind_Image/";
        string urlPath = "http://localhost:8099/";//浏览路径

        //定义允许上传的文件扩展名
        Hashtable extTable = new Hashtable();
        extTable.Add("image", "gif,jpg,jpeg,png,bmp");
        extTable.Add("flash", "swf,flv");
        extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
        extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

        //最大文件大小
        int maxSize = 1000000;
        this.context = context;

        HttpPostedFile imgFile = context.Request.Files["imgFile"];
        if (imgFile == null)
        {
            showError("请选择文件。");
        }
          
        String dirName = context.Request.QueryString["dir"];//好像没什么必要,但是必须有
        
        
        String fileName = imgFile.FileName;
        String fileExt = Path.GetExtension(fileName).ToLower();

        if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
        {
            showError("上传文件大小超过限制。");
        }

        if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
        {
            showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
        }

        //创建文件夹
        String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
        savePath += ymd + "/";
    
        if (!Directory.Exists(savePath)) {
            Directory.CreateDirectory(savePath);
        }

        String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
        String filePath = savePath + newFileName;

        imgFile.SaveAs(filePath);

        String fileUrl = urlPath + ymd + "/" + newFileName;

        Hashtable hash = new Hashtable();
        hash["error"] = 0;
        hash["url"] = fileUrl;
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(JsonMapper.ToJson(hash));
        context.Response.End();
    }

    private void showError(string message)
    {
        Hashtable hash = new Hashtable();
        hash["error"] = 1;
        hash["message"] = message;
        context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(JsonMapper.ToJson(hash));
        context.Response.End();
    }

原创粉丝点击