低版本net项目使用ueditor 图片上传配置

写着玩,不喜勿喷

1.开发工具vs2010

* UEditor Mini版本  version: 1.2.2

最近碰到这样一个问题,可能因为项目版本太低,下载最新的ueditor(1.4.3),项目一直编译不通过,

而且配置按顺序在 umeditor.config.js 配置了统一的请求


,请求返回状态为200,但是不返回配置文件读取的json,郁闷至极

(个人声明,重新搭建项目,使用同样的配置,一直都是通过的,偏偏今天遇到鬼了)

有的人说还有引用4.0,但是项目只允许使用3.5,

折腾半天,找了个低版本的UEditor( Mini版本  version: 1.2.2),配置如下,其实就是配置了imageurl


,使用自带的程序还是不成功,算了,自己写一个hander,把自带的程序改改就好了

-----------------------------------------------------------------------------------注:我没有把图片保存到本地,是把图片转换了一下,人家要求是不要直接保存文件,要使用二进制文件流,我就这要搞了,没有这要需求的同学记得改一下---------------------------------------------------------------------------------------------------------------------

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        //上传配置
        string pathbase = "/_Images/upload/";                                                          //保存路径
        int size = 10;                     //文件大小限制,单位mb                                                                                   //文件大小限制,单位KB
        string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };                    //文件允许格式

        string callback = context.Request["callback"];
        string editorId = context.Request["editorid"];

        
        Stream  stream = context.Request.Files[0].InputStream;
        
      //   byte[] bytes = new byte[stream.Length];
     //    stream.Read(bytes, 0, bytes.Length);
        // 设置当前流的位置为流的开始
     //   stream.Seek(0, SeekOrigin.Begin);
     //   string base64 ="data:image/png;base64,"+ Convert.ToBase64String(bytes);

        //上传图片
        Hashtable info;
        Uploader up = new Uploader();
        info = up.upFile(context, pathbase, filetype, size); //获取上传状态
        string json = BuildJson(info);
    
        context.Response.ContentType = "text/html";
        if (callback != null)
        {
            context.Response.Write(String.Format("<script>{0}(JSON.parse(\"{1}\"));</script>", callback, json));
        }
        else
        {
            context.Response.Write(json);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private string BuildJson(Hashtable info)
    {
        List<string> fields = new List<string>();
        string[] keys = new string[] { "originalName", "name", "url", "size", "state", "type" };
        for (int i = 0; i < keys.Length; i++)
        {
          
            fields.Add(String.Format("\"{0}\": \"{1}\"", keys[i], info[keys[i]]));
        }

        return "{" + String.Join(",", fields.ToArray()) + "}";
    }
}


/// <summary>
/// UEditor编辑器通用上传类
/// </summary>
public class Uploader
{
    string state = "SUCCESS";

    string URL = null;
    string currentType = null;
    string uploadpath = null;
    string filename = null;
    string originalName = null;
    HttpPostedFile uploadFile = null;

    /**
  * 上传文件的主处理方法
  * @param HttpContext
  * @param string
  * @param  string[]
  *@param int
  * @return Hashtable
  */
    public Hashtable upFile(HttpContext cxt, string pathbase, string[] filetype, int size)
    {
        pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径

        try
        {
            uploadFile = cxt.Request.Files[0];
            originalName = uploadFile.FileName;

            //目录创建
            createFolder();

            //格式验证
            if (checkType(filetype))
            {
                state = "不允许的文件类型";
            }
            //大小验证
            if (checkSize(size))
            {
                state = "文件大小超出网站限制";
            }
            //保存图片
            if (state == "SUCCESS")
            {

                Stream stream = uploadFile.InputStream;
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始
                stream.Seek(0, SeekOrigin.Begin);
                URL = "data:image/png;base64," + Convert.ToBase64String(bytes);
            }
        }
        catch (Exception e)
        {
            state = "未知错误";
            URL = "";
        }
        return getUploadInfo();
    }

    /**
 * 上传涂鸦的主处理方法
  * @param HttpContext
  * @param string
  * @param  string[]
  *@param string
  * @return Hashtable
 */
    public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
    {
        pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
        FileStream fs = null;
        try
        {
            //创建目录
            createFolder();
            //生成图片
            filename = System.Guid.NewGuid() + ".png";
            fs = File.Create(uploadpath + filename);
            byte[] bytes = Convert.FromBase64String(base64Data);
            fs.Write(bytes, 0, bytes.Length);

            URL = pathbase + filename;
        }
        catch (Exception e)
        {
            state = "未知错误";
            URL = "";
        }
        finally
        {
            fs.Close();
            deleteFolder(cxt.Server.MapPath(tmppath));
        }
        return getUploadInfo();
    }

    /**
* 获取文件信息
* @param context
* @param string
* @return string
*/
    public string getOtherInfo(HttpContext cxt, string field)
    {
        string info = null;
        if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
        {
            info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
        }
        return info;
    }

    /**
     * 获取上传信息
     * @return Hashtable
     */
    private Hashtable getUploadInfo()
    {
        Hashtable infoList = new Hashtable();

        infoList.Add("state", state);
        infoList.Add("url", URL);
        infoList.Add("originalName", originalName);
        infoList.Add("name", Path.GetFileName(URL));
        infoList.Add("size", uploadFile.ContentLength);
        infoList.Add("type", Path.GetExtension(originalName));

        return infoList;
    }

    /**
     * 重命名文件
     * @return string
     */
    private string reName()
    {
        return System.Guid.NewGuid() + getFileExt();
    }

    /**
     * 文件类型检测
     * @return bool
     */
    private bool checkType(string[] filetype)
    {
        currentType = getFileExt();
        return Array.IndexOf(filetype, currentType) == -1;
    }

    /**
     * 文件大小检测
     * @param int
     * @return bool
     */
    private bool checkSize(int size)
    {
        return uploadFile.ContentLength >= (size * 1024 * 1024);
    }

    /**
     * 获取文件扩展名
     * @return string
     */
    private string getFileExt()
    {
        string[] temp = uploadFile.FileName.Split('.');
        return "." + temp[temp.Length - 1].ToLower();
    }

    /**
     * 按照日期自动创建存储文件夹
     */
    private void createFolder()
    {
        if (!Directory.Exists(uploadpath))
        {
            Directory.CreateDirectory(uploadpath);
        }
    }

    /**
     * 删除存储文件夹
     * @param string
     */
    public void deleteFolder(string path)
    {
        //if (Directory.Exists(path))
        //{
        //    Directory.Delete(path, true);
        //}
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值