zoukankan      html  css  js  c++  java
  • UEditor上传文件的默认地址修改

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

    /// <summary>
    /// UploadHandler 的摘要说明
    /// </summary>
    public class UploadHandler : Handler
    {

    public UploadConfig UploadConfig { get; private set; }
    public UploadResult Result { get; private set; }

    public UploadHandler(HttpContext context, UploadConfig config)
    : base(context)
    {
    this.UploadConfig = config;
    this.Result = new UploadResult() { State = UploadState.Unknown };
    }

    public override void Process()
    {
    byte[] uploadFileBytes = null;
    string uploadFileName = null;

    if (UploadConfig.Base64)
    {
    uploadFileName = UploadConfig.Base64Filename;
    uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
    }
    else
    {
    var file = Request.Files[UploadConfig.UploadFieldName];
    uploadFileName = file.FileName;

    if (!CheckFileType(uploadFileName))
    {
    Result.State = UploadState.TypeNotAllow;
    WriteResult();
    return;
    }
    if (!CheckFileSize(file.ContentLength))
    {
    Result.State = UploadState.SizeLimitExceed;
    WriteResult();
    return;
    }

    uploadFileBytes = new byte[file.ContentLength];
    try
    {
    file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
    }
    catch (Exception)
    {
    Result.State = UploadState.NetworkError;
    WriteResult();
    }
    }

    Result.OriginFileName = uploadFileName;

    var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
    //UploadConfig.PathFormat=D:/UploadFiles/Hospital/{yyyy}{mm}{dd}/{time}{rand:6}
    //savePath =D:/UploadFiles/Hospital/20151027/6358155709153650499167466.png
    var localPath = savePath;// Server.MapPath(savePath);
    try
    {
    #region UEditor本身
    //if (!Directory.Exists(Path.GetDirectoryName(localPath)))
    //{
    // Directory.CreateDirectory(Path.GetDirectoryName(localPath));
    //}
    //File.WriteAllBytes(localPath, uploadFileBytes);

    //////压缩图片大小
    ////System.Drawing.Image originalImage = System.Drawing.Image.FromFile(localPath);
    ////int w = originalImage.Width;
    ////int h = originalImage.Height;
    ////originalImage.Dispose();
    ////返回路径
    //Result.Url = savePath.Substring(UploadConfig.PathFormat.IndexOf('{'));// savePath;
    //Result.State = UploadState.Success;
    #endregion

    #region 调用接口上传图片修改
    //调用接口上传图片
    string picUrl = string.Empty;

    FormItemModel formInfo = new FormItemModel() //提交文件参数
    {
    Key = "file",
    Value = string.Empty,
    FileName = uploadFileName,
    FileContent = new MemoryStream(uploadFileBytes)
    };

    ResponseMessageInfo responseInfo = Common.HttpPostFuns.Instance().PostFile(Common.CommonConst.PlatUploadFileUrl, Common.CommonConst.OperationBackProjectCode, formInfo);

    if (responseInfo.code != "200")
    {
    throw new Common.BizTipsException(responseInfo.code, responseInfo.message);
    }

    System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
    var jArr = Newtonsoft.Json.Linq.JArray.Parse(jss.Serialize(responseInfo.responseData));
    if (jArr.Count > 0)
    {
    Newtonsoft.Json.Linq.JObject jo = (Newtonsoft.Json.Linq.JObject)jArr[0];
    picUrl = jo["url"].ToString();
    }
    //Result.Url = picUrl.Substring(UploadConfig.PathFormat.IndexOf('{'));// savePath;
    Result.Url = picUrl;
    Result.State = UploadState.Success;
    #endregion

    }
    catch (Exception e)
    {
    Result.State = UploadState.FileAccessError;
    Result.ErrorMessage = e.Message;
    }
    finally
    {
    WriteResult();
    }
    }

    private void WriteResult()
    {
    this.WriteJson(new
    {
    state = GetStateMessage(Result.State),
    url = Result.Url,
    title = Result.OriginFileName,
    original = Result.OriginFileName,
    error = Result.ErrorMessage
    });
    }

    private string GetStateMessage(UploadState state)
    {
    switch (state)
    {
    case UploadState.Success:
    return "SUCCESS";
    case UploadState.FileAccessError:
    return "文件访问出错,请检查写入权限";
    case UploadState.SizeLimitExceed:
    return "文件大小超出服务器限制";
    case UploadState.TypeNotAllow:
    return "不允许的文件格式";
    case UploadState.NetworkError:
    return "网络错误";
    }
    return "未知错误";
    }

    private bool CheckFileType(string filename)
    {
    var fileExtension = Path.GetExtension(filename).ToLower();
    return UploadConfig.AllowExtensions.Select(x => x.ToLower()).Contains(fileExtension);
    }

    private bool CheckFileSize(int size)
    {
    return size < UploadConfig.SizeLimit;
    }
    }

    public class UploadConfig
    {
    /// <summary>
    /// 文件命名规则
    /// </summary>
    public string PathFormat { get; set; }

    /// <summary>
    /// 上传表单域名称
    /// </summary>
    public string UploadFieldName { get; set; }

    /// <summary>
    /// 上传大小限制
    /// </summary>
    public int SizeLimit { get; set; }

    /// <summary>
    /// 上传允许的文件格式
    /// </summary>
    public string[] AllowExtensions { get; set; }

    /// <summary>
    /// 文件是否以 Base64 的形式上传
    /// </summary>
    public bool Base64 { get; set; }

    /// <summary>
    /// Base64 字符串所表示的文件名
    /// </summary>
    public string Base64Filename { get; set; }
    }

    public class UploadResult
    {
    public UploadState State { get; set; }
    public string Url { get; set; }
    public string OriginFileName { get; set; }

    public string ErrorMessage { get; set; }
    }

    public enum UploadState
    {
    Success = 0,
    SizeLimitExceed = -1,
    TypeNotAllow = -2,
    FileAccessError = -3,
    NetworkError = -4,
    Unknown = 1,
    }

  • 相关阅读:
    zabbix 添加 微信、邮件 媒介详解
    zabbix使用之常用功能使用心得
    Nginx1.8源码包编译安装
    httpd启动显示Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName'
    apache-httpd2.4编译安装
    apache-httpd2.2编译安装
    MYSQL5.7源码包编译安装
    MYSQL常见安装错误集:[ERROR] --initialize specified but the data directory has files in it. Abort
    MYSQL启用数据库错误:ERROR 2002 (HY000)
    编译mysql时CMake Error at cmake/readline.cmake:85 (MESSAGE)
  • 原文地址:https://www.cnblogs.com/zhoading/p/8037941.html
Copyright © 2011-2022 走看看