public IActionResult Post() { var files = Request.Form.Files; long size = files.Sum(f => f.Length); //size > 100MB refuse upload ! if (size > 104857600) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode("pictures total size > 100MB , server refused !"))); } List <string> filePathResultList = new List <string>(); foreach (var file in files) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string filePath = hostingEnv.WebRootPath + $@"\Files\Pictures\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } //string suffix = fileName.Split('.')[1]; string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1, (fileName.Length - fileName.LastIndexOf(".") - 1)); if (!pictureFormatArray.Contains(suffix)) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode("the picture format not support ! you must upload files that suffix like 'png','jpg','jpeg','bmp','gif','ico'."))); } fileName = Guid.NewGuid() + "." + suffix; string fileFullName = filePath + fileName; using (FileStream fs = System.IO.File.Create(fileFullName)) { file.CopyTo(fs); fs.Flush(); } filePathResultList.Add($"/src/Pictures/{fileName}"); } string message = $"{files.Count} file(s) /{size} bytes uploaded successfully!"; return(Json(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode(message, filePathResultList, filePathResultList.Count))); }
public IActionResult Post() { var files = Request.Form.Files; long size = files.Sum(f => f.Length); //size > 100MB refuse upload ! if (size > 104857600) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode("files total size > 100MB , server refused !"))); } List <string> filePathResultList = new List <string>(); foreach (var file in files) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string filePath = hostingEnv.WebRootPath + $@"\Files\Files\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } fileName = Guid.NewGuid() + "." + fileName.Split('.')[1]; string fileFullName = filePath + fileName; using (FileStream fs = System.IO.File.Create(fileFullName)) { file.CopyTo(fs); fs.Flush(); } filePathResultList.Add($"/src/Files/{fileName}"); } string message = $"{files.Count} file(s) /{size} bytes uploaded successfully!"; return(Json(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode(message, filePathResultList, filePathResultList.Count))); }
public IHttpActionResult DeleteQX(dynamic query) { try { var list = new List <dynamic>(); list.Add(new { ID = "aaaa", NO = "111", NAME = "1111", DESC = "11111" }); list.Add(new { ID = "bbbb", NO = "222", NAME = "2222", DESC = "22222" }); list.Add(new { ID = "cccc", NO = "333", NAME = "3333", DESC = "33333" }); list.Add(new { ID = "dddd", NO = "444", NAME = "4444", DESC = "44444" }); list.Remove(list.Where(li => li.ID == query.ID.ToString()).FirstOrDefault()); //var result = new { TFMark = true, Msg = "this is HttpDELETE Method", list = list }; //return Json<dynamic>(result); return(Json <dynamic>(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode("this is HttpDELETE Method", list, list.Count))); } catch (Exception ex) { return(Ok(ex)); } }
public override void OnException(HttpActionExecutedContext actionExecutedContext) { Log_Helper_DG.Log_Error($"{actionExecutedContext.Exception.Message} -- error : {actionExecutedContext.Exception.StackTrace} ", $"{actionExecutedContext.Exception.GetType().ToString()}"); string Message = actionExecutedContext.Exception.Message; HttpStatusCode HttpCode = HttpStatusCode.InternalServerError; //the default HttpStatusCode int ErrorCode = 0; int ErrorLevel = 0; if (actionExecutedContext.Exception is Exception_DG) { Exception_DG exception = actionExecutedContext.Exception as Exception_DG; //实例化一个T类型对象 ErrorCode = exception.ErrorCode; ErrorLevel = exception.ErrorLevel; if (!string.IsNullOrEmpty(exception.Arguments)) { Message = Message + " Arguments:" + exception.Arguments; } } else if (actionExecutedContext.Exception is Exception_DG_Internationalization) { Exception_DG_Internationalization exception = actionExecutedContext.Exception as Exception_DG_Internationalization; //实例化一个T类型对象 ErrorCode = exception.ErrorCode; ErrorLevel = exception.ErrorLevel; Message = exception.Message_DG; if (!string.IsNullOrEmpty(exception.Arguments)) { Message = Message + " Arguments:" + exception.Arguments; } } else if (actionExecutedContext.Exception is NotImplementedException) { HttpCode = HttpStatusCode.NotImplemented; } else if (actionExecutedContext.Exception is TimeoutException) { HttpCode = HttpStatusCode.RequestTimeout; } else if (actionExecutedContext.Exception is ArgumentException) { HttpCode = HttpStatusCode.MethodNotAllowed; } else if (actionExecutedContext.Exception is System.IO.FileNotFoundException) { HttpCode = HttpStatusCode.NotFound; } //..... else { HttpCode = HttpStatusCode.InternalServerError; } object ErrorObject = Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode($"{Message}", ErrorCode, ErrorLevel, HttpCode); actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(ErrorObject); base.OnException(actionExecutedContext); }
public IActionResult UpImg([FromForm] IFormCollection formData) { try { IFormFileCollection files = formData.Files; var ImgId = Request.Form["ImgId"]; var Imgtype = Request.Form["Imgtype"]; var userData = new UserInfoDataBase(); long size = files.Sum(f => f.Length); //size > 100MB refuse upload ! if (size > 104857600) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode("图片总共大小不能超过 100MB "))); } List <string> filePathResultList = new List <string>(); string filePathd = hostingEnv.WebRootPath; //+ $@"\Files\Pictures\"; if (!System.IO.Directory.Exists(filePathd + "/Files")) { System.IO.Directory.CreateDirectory(filePathd + "/Files"); } if (!System.IO.Directory.Exists(filePathd + "/Files/Picture/")) { System.IO.Directory.CreateDirectory(filePathd + "/Files//Pictures/"); } foreach (var file in files) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string filePath = hostingEnv.WebRootPath + "/Files/Pictures/"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string suffix = fileName.Split('.')[1]; if (!pictureFormatArray.Contains(suffix)) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode("当前图片格式不支持"))); } fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff").Replace('0', '1') + "." + suffix; string fileFullName = filePath + fileName; var entit = new Img(); entit.Id = Guid.NewGuid(); entit.UserInfoId = Guid.Parse(ImgId); entit.CreateTime = DateTime.Now; entit.Type = Convert.ToInt16(Imgtype); entit.Url = "/Files/Pictures/" + fileName; //保存数据 userData.AddImg(entit); using (FileStream fs = System.IO.File.Create(fileFullName)) { file.CopyTo(fs); fs.Flush(); } filePathResultList.Add(fileFullName); } string message = $"上传成功"; return(Json(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode(message, filePathResultList, filePathResultList.Count))); } catch (Exception e) { throw e; } }
/// <summary> /// ERROR /// </summary> /// <param name="msg"></param> /// <param name="errorCode"></param> /// <param name="errorLevel"></param> /// <param name="httpCode"></param> /// <returns></returns> protected JsonResult ERROR(string msg, int errorCode = 0, int errorLevel = 0, System.Net.HttpStatusCode httpCode = System.Net.HttpStatusCode.InternalServerError) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode(msg, errorCode, errorLevel, httpCode), JsonRequestBehavior.AllowGet)); }
/// <summary> /// OK /// </summary> /// <param name="msg"></param> /// <param name="data"></param> /// <param name="dataCount"></param> /// <param name="httpCode"></param> /// <returns></returns> protected JsonResult OK(string msg, dynamic data = null, int dataCount = 0, System.Net.HttpStatusCode httpCode = System.Net.HttpStatusCode.OK) { return(Json(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode(msg, data, dataCount, httpCode), JsonRequestBehavior.AllowGet)); }
/// <summary> /// ERROR /// </summary> /// <param name="msg"></param> /// <param name="errorCode"></param> /// <param name="errorLevel"></param> /// <param name="httpCode"></param> /// <returns></returns> protected IHttpActionResult ERROR(string msg, int errorCode = 0, int errorLevel = 0, System.Net.HttpStatusCode httpCode = System.Net.HttpStatusCode.InternalServerError) { return(Json(Return_Helper_DG.Error_Msg_Ecode_Elevel_HttpCode(msg, errorCode, errorLevel, httpCode))); }
/// <summary> /// OK /// </summary> /// <param name="msg"></param> /// <param name="data"></param> /// <param name="dataCount"></param> /// <param name="httpCode"></param> /// <returns></returns> protected IHttpActionResult OK(string msg, dynamic data = null, int dataCount = 0, System.Net.HttpStatusCode httpCode = System.Net.HttpStatusCode.OK) { return(Json(Return_Helper_DG.Success_Msg_Data_DCount_HttpCode(msg, data, dataCount, httpCode))); }