加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

ueditor上传图片配置

发布时间:2020-12-15 00:19:28 所属栏目:C语言 来源:网络整理
导读://为编辑器实例添加一个路径,这个不能被注释 UEDITOR_HOME_URL: URL // 服务器统一请求接口路径,serverUrl: URL + "net/controller.ashx"/code/pre controller.ashx using System; using System.Configuration; using System.Text.RegularExpressions; usin
 //为编辑器实例添加一个路径,这个不能被注释
        UEDITOR_HOME_URL: URL
    // 服务器统一请求接口路径,serverUrl: URL + "net/controller.ashx"</code></pre>

controller.ashx

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

using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.Collections;

public class UEditorHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
byte[] uploadFileBytes = null;
string uploadFileName = null;
var json = "";
switch (context.Request["action"])
{
case "config":
json = "{"imageActionName":"uploadimage","imageFieldName": "upfile","imageCompressEnable":"true","imageCompressBorder": 1600,"imageInsertAlign": "none","imageUrlPrefix": "","imageAllowFiles": [".png",".jpg",".jpeg",".gif",".bmp"]}";
break;
case "uploadimage":
var file = context.Request.Files["upfile"];
uploadFileName = file.FileName;
var path = ConfigurationManager.AppSettings["UploadPath"];
var savePath = Format(uploadFileName,path+"{yyyy}{mm}{dd}/{time}{rand:6}");
var localPath = context.Server.MapPath(savePath);

                if (!Directory.Exists(Path.GetDirectoryName(localPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                }
                uploadFileBytes = new byte[file.ContentLength];
                file.InputStream.Read(uploadFileBytes,file.ContentLength);
                File.WriteAllBytes(localPath,uploadFileBytes);
                json = json + "{"url":"" + savePath.Replace("~","") + "",";
                json = json + ""state":"SUCCESS"}";

            //catch (Exception e)
            //{
            //    Result.State = UploadState.FileAccessError;
            //    Result.ErrorMessage = e.Message;
            //}
            //finally
            //{
            //    WriteResult();
            //}
            //        break;
            //}
            //action.Process();
            break;
    }
    WriteJson(context,json);
}

public bool IsReusable
{
    get
    {
        return false;
    }
}
protected void WriteJson(HttpContext context,string json)
{
    string jsonpCallback = context.Request["callback"];
    if (String.IsNullOrWhiteSpace(jsonpCallback))
    {
        context.Response.AddHeader("Content-Type","text/plain");
        context.Response.Write(json);
    }
    else 
    {
        context.Response.AddHeader("Content-Type","application/javascript");
        context.Response.Write(String.Format("{0}({1});",jsonpCallback,json));
    }
    context.Response.End();
}
private string Format(string originFileName,string pathFormat)
{
    if (String.IsNullOrWhiteSpace(pathFormat))
    {
        pathFormat = "{filename}{rand:6}";
    }

    var invalidPattern = new Regex(@"[/:*?42&;&;|]");
    originFileName = invalidPattern.Replace(originFileName,"");

    string extension = Path.GetExtension(originFileName);
    string filename = Path.GetFileNameWithoutExtension(originFileName);

    pathFormat = pathFormat.Replace("{filename}",filename);
    pathFormat = new Regex(@"{rand(:?)(d+)}",RegexOptions.Compiled).Replace(pathFormat,new MatchEvaluator(delegate(Match match)
    {
        var digit = 6;
        if (match.Groups.Count > 2)
        {
            digit = Convert.ToInt32(match.Groups[2].Value);
        }
        var rand = new Random();
        return rand.Next((int)Math.Pow(10,digit),(int)Math.Pow(10,digit + 1)).ToString();
    }));

    pathFormat = pathFormat.Replace("{time}",DateTime.Now.Ticks.ToString());
    pathFormat = pathFormat.Replace("{yyyy}",DateTime.Now.Year.ToString());
    pathFormat = pathFormat.Replace("{yy}",(DateTime.Now.Year % 100).ToString("D2"));
    pathFormat = pathFormat.Replace("{mm}",DateTime.Now.Month.ToString("D2"));
    pathFormat = pathFormat.Replace("{dd}",DateTime.Now.Day.ToString("D2"));
    pathFormat = pathFormat.Replace("{hh}",DateTime.Now.Hour.ToString("D2"));
    pathFormat = pathFormat.Replace("{ii}",DateTime.Now.Minute.ToString("D2"));
    pathFormat = pathFormat.Replace("{ss}",DateTime.Now.Second.ToString("D2"));

    return pathFormat + extension;
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读