/// <summary> /// 编辑器状态 /// </summary> /// <param name="State">标识</param> /// <returns></returns> public static string GetStateString(DTEnums.ResultState State) { switch (State) { case DTEnums.ResultState.Success: return("SUCCESS"); case DTEnums.ResultState.InvalidParam: return("参数不正确"); case DTEnums.ResultState.PathNotFound: return("路径不存在"); case DTEnums.ResultState.AuthorizError: return("文件系统权限不足"); case DTEnums.ResultState.IOError: return("文件系统读取错误"); case DTEnums.ResultState.FileAccessError: return("文件访问出错,请检查写入权限"); case DTEnums.ResultState.SizeLimitExceed: return("文件大小超出服务器限制"); case DTEnums.ResultState.TypeNotAllow: return("不允许的文件格式"); case DTEnums.ResultState.NetworkError: return("网络错误"); } return("未知错误"); }
/// <summary> /// 读取文件列表 /// </summary> /// <param name="listPath"></param> /// <param name="allow">类型 0图片,1文件</param> private void ListFileManager(HttpContext context, string listPath, int allow) { int Start = DTRequest.GetQueryIntValue("start", 0); int Size = DTRequest.GetQueryIntValue("size", 20); int Total = 0; DTEnums.ResultState State = DTEnums.ResultState.Success; String PathToList = siteConfig.webpath + siteConfig.filepath; String[] FileList = null; String[] SearchExtensions; if (allow > 0) { //文件 SearchExtensions = Utils.MergerArray(siteConfig.fileextension.Split(','), siteConfig.videoextension.Split(',')); } else { //图片 SearchExtensions = new string[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; } var buildingList = new List <String>(); try { var localPath = Utils.GetMapPath(PathToList); buildingList.AddRange(Directory.GetFiles(localPath, "*", SearchOption.AllDirectories) .Where(x => SearchExtensions.Contains(Path.GetExtension(x).ToLower())) .Select(x => PathToList + x.Substring(localPath.Length).Replace("\\", "/"))); Total = buildingList.Count; FileList = buildingList.OrderBy(x => x).Skip(Start).Take(Size).ToArray(); } catch (UnauthorizedAccessException) { State = DTEnums.ResultState.AuthorizError; } catch (DirectoryNotFoundException) { State = DTEnums.ResultState.PathNotFound; } catch (IOException) { State = DTEnums.ResultState.IOError; } finally { JsonHelper.WriteJson(context, new { state = Utils.GetStateString(State), list = FileList == null ? null : FileList.Select(x => new { url = x }), start = Start, size = Size, total = Total }); } }
private void AdminUploadHandler(HttpContext context, string name) { //检查用户是否登录 DTcms.Model.manager modelAdmin = new DTcms.Web.UI.ManagePage().GetAdminInfo(); if (modelAdmin == null) { JsonHelper.WriteJson(context, new { state = DTEnums.ResultState.NetworkError, error = "对不起,用户尚未登录或已超时!" }); return; } string fieldName = "upfile"; string[] allowExtensions = null; int sizeLimit = 0; switch (name) { case "image": sizeLimit = siteConfig.imgsize; allowExtensions = siteConfig.fileextension.Split(','); //siteConfig.imgextension.Split(','); break; case "scrawl": sizeLimit = 2048000; allowExtensions = new string[] { ".png" }; break; case "video": sizeLimit = siteConfig.videosize; allowExtensions = siteConfig.videoextension.Split(','); break; case "file": sizeLimit = siteConfig.attachsize; allowExtensions = siteConfig.fileextension.Split(','); break; default: JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (sizeLimit <= 0 || allowExtensions.Length == 0) { JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (name == "scrawl") { string uploadFileName = Utils.GetRamCode() + ".png"; byte[] uploadFileBytes = Convert.FromBase64String(context.Request[fieldName]); string Url = new UpLoad().GetUpLoadPath() + uploadFileName; string localPath = Utils.GetMapPath(Url); string ErrorMessage = string.Empty; DTEnums.ResultState State = DTEnums.ResultState.Success; try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); State = DTEnums.ResultState.Success; } catch (Exception e) { State = DTEnums.ResultState.FileAccessError; ErrorMessage = e.Message; } JsonHelper.WriteJson(context, new { state = Utils.GetStateString(State), url = Url, title = uploadFileName, original = uploadFileName, error = ErrorMessage }); } else { context.Response.ContentType = "text/plain"; HttpPostedFile _upfile = context.Request.Files[fieldName]; Model.upLoad model = new UpLoad().fileSaveAs(_upfile, allowExtensions, sizeLimit, false, false, 0, 0); if (model.status > 0) { //{"state":"SUCCESS","url":"/upload/201612/22/201612221435403316.jpg","title":"未命名-1.jpg","original":"未命名-1.jpg","error":""} JsonHelper.WriteJson(context, new { state = Utils.GetStateString(DTEnums.ResultState.Success), url = model.path, title = model.name, original = model.name, error = "" }); } else if (model.status == -1) { JsonHelper.WriteJson(context, new { state = DTEnums.ResultState.SizeLimitExceed, error = Utils.GetStateString(DTEnums.ResultState.SizeLimitExceed) }); } else if (model.status == -2) { JsonHelper.WriteJson(context, new { state = DTEnums.ResultState.TypeNotAllow, error = Utils.GetStateString(DTEnums.ResultState.TypeNotAllow) }); } else if (model.status == -3) { JsonHelper.WriteJson(context, new { state = DTEnums.ResultState.NetworkError, error = Utils.GetStateString(DTEnums.ResultState.NetworkError) }); } } }
private void EditorHandler(HttpContext context, string name) { //检查用户是否登录 modelUser = new Page.ForumPage().GetOnlineUser(); if (modelUser == null) { context.Response.Write("{\"status\":0, \"msg\":\"对不起,用户尚未登录或已超时!\"}"); return; } string fieldName = "upfile"; string[] allowExtensions = null; int sizeLimit = 0; switch (name) { case "image": sizeLimit = siteConfig.imgsize; allowExtensions = siteConfig.fileextension.Split(','); break; case "scrawl": sizeLimit = 2048000; allowExtensions = new string[] { ".png" }; break; case "video": sizeLimit = siteConfig.videosize; allowExtensions = siteConfig.videoextension.Split(','); break; case "file": sizeLimit = siteConfig.attachsize; allowExtensions = siteConfig.fileextension.Split(','); break; default: JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (sizeLimit <= 0 || allowExtensions.Length == 0) { JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (name == "scrawl") { string uploadFileName = Utils.GetRamCode() + ".png"; byte[] uploadFileBytes = Convert.FromBase64String(context.Request[fieldName]); string Url = new UpLoad().GetUpLoadPath() + uploadFileName; string localPath = Utils.GetMapPath(Url); string ErrorMessage = string.Empty; DTEnums.ResultState State = DTEnums.ResultState.Success; try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); State = DTEnums.ResultState.Success; } catch (Exception e) { State = DTEnums.ResultState.FileAccessError; ErrorMessage = e.Message; } JsonHelper.WriteJson(context, new { state = Utils.GetStateString(State), url = Url, title = uploadFileName, original = uploadFileName, error = ErrorMessage }); } else { context.Response.ContentType = "text/plain"; HttpPostedFile _upfile = context.Request.Files[fieldName]; Model.upLoad model = new UpLoad().fileSaveAs(_upfile, allowExtensions, sizeLimit, false, false, 0, 0); if (model.status > 0) { //{"aid":374,"name":"27.gif","filename":"2016/11/02/4225885467939.gif","isimage":true} Model.Forum_Attachment attModel = new Model.Forum_Attachment(); attModel.IsImage = 1; attModel.Name = model.name; attModel.BoardId = board_id; attModel.TopicId = topic_id; attModel.PostId = post_id; attModel.FileName = model.path; attModel.FileSize = model.size / 1024;; attModel.FileType = model.ext.Replace(".", ""); attModel.UserId = modelUser.UserId; attModel.Id = new BLL.Forum_Attachment().Add(attModel); string callback = context.Request["callback"]; string editorId = context.Request["editorid"]; JsonHelper.WriteJson(context, new { state = "SUCCESS", type = "." + model.ext, size = model.size / 1024, url = model.path, name = model.name, originalName = model.name }); } else { JsonHelper.WriteJson(context, new { status = model.msg }); } } }
private void UploadHandler(HttpContext context, string name) { string fieldName = "upfile"; string[] allowExtensions = null; int sizeLimit = 0; switch (name) { case "image": sizeLimit = siteConfig.imgsize; allowExtensions = siteConfig.imgextension.Split(','); break; case "scrawl": sizeLimit = 2048000; allowExtensions = new string[] { ".png" }; break; case "video": sizeLimit = siteConfig.videosize; allowExtensions = siteConfig.videoextension.Split(','); break; case "file": sizeLimit = siteConfig.attachsize; allowExtensions = siteConfig.fileextension.Split(','); break; default: JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (sizeLimit <= 0 || allowExtensions.Length == 0) { JsonHelper.WriteJson(context, new { status = "参数传输错误!" }); return; } if (name == "scrawl") { string uploadFileName = Utils.GetRamCode() + ".png"; byte[] uploadFileBytes = Convert.FromBase64String(context.Request[fieldName]); string Url = new UpLoad().GetUpLoadPath() + uploadFileName; string localPath = Utils.GetMapPath(Url); string ErrorMessage = string.Empty; DTEnums.ResultState State = DTEnums.ResultState.Success; try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); State = DTEnums.ResultState.Success; } catch (Exception e) { State = DTEnums.ResultState.FileAccessError; ErrorMessage = e.Message; } JsonHelper.WriteJson(context, new { state = Utils.GetStateString(State), url = Url, title = uploadFileName, original = uploadFileName, error = ErrorMessage }); } else { context.Response.ContentType = "text/plain"; HttpPostedFile _upfile = context.Request.Files[fieldName]; Model.upLoad model = new UpLoad().fileSaveAs(_upfile, allowExtensions, sizeLimit, false, false, 0, 0); if (model.status > 0) { JsonHelper.WriteJson(context, new { state = "SUCCESS", url = model.path, title = model.name, original = model.name, error = "" }); } else { JsonHelper.WriteJson(context, new { status = model.msg }); } } }