public override void OnException(HttpActionExecutedContext actionExecutedContext) { UnifiedResultEntity <string> model = new UnifiedResultEntity <string>(); model.msg = 0; model.msgbox = actionExecutedContext.Exception.Message; model.data = ""; actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.OK, model); base.OnException(actionExecutedContext); }
/// <summary> /// 上传文件-处理base64位图片数据的方法 /// </summary> /// <param name="base64"></param> /// <param name="save_path"></param> /// <returns></returns> public UnifiedResultEntity <string> Upload_For_Base64Data(string base64, string ServerPath) { UnifiedResultEntity <string> response_entity = new UnifiedResultEntity <string>(); response_entity.msg = 0; if (string.IsNullOrWhiteSpace(base64) || string.IsNullOrWhiteSpace(ServerPath)) { response_entity.msgbox = "缺少参数"; return(response_entity); } //保存的绝对路径,不包含文件名 string filepath_no_name = IOHelper.GetMapPath(ServerPath); try { string guid = Guid.NewGuid().ToString(); string saveName = guid;// +".JPG"; // 保存文件名称 string file_ext = ""; string file_path = IOHelper.Base64ToImgSave(base64, ServerPath, saveName, out file_ext); if (string.IsNullOrWhiteSpace(file_path) || string.IsNullOrWhiteSpace(file_ext)) { response_entity.msgbox = "文件保存失败"; return(response_entity); } saveName = saveName + "." + file_ext; //保存后文件的绝对路径,包括文件名 string filePath = IOHelper.GetMapPath(file_path); //获取MD5值 string md5 = IOHelper.GetMD5HashFromFile(filePath); if (System.IO.File.Exists(filepath_no_name + md5 + "." + file_ext)) { System.IO.File.Delete(filePath); //把刚刚上传的给删掉,只用原有的文件 } else //不存在,改名为md5值保存 { System.IO.File.Move(filePath, filepath_no_name + md5 + "." + file_ext); //给文件改名 } response_entity.msg = 1; response_entity.msgbox = "上传成功"; response_entity.data = ServerPath + md5 + "." + file_ext; return(response_entity); } catch (Exception ex) { response_entity.msgbox = ex.Message; return(response_entity); } }
public override void OnException(HttpActionExecutedContext actionExecutedContext) { //异常信息 string error_msg = actionExecutedContext.Exception.Message; ExceptionInDB.ToInDB(actionExecutedContext.Exception); UnifiedResultEntity <string> model = new UnifiedResultEntity <string>(); model.msgbox = error_msg; actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.OK, model); base.OnException(actionExecutedContext); }
/// <summary> /// 取消请求 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <param name="message"></param> /// <returns></returns> private Task <HttpResponseMessage> requestCancel(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken, string message) { UnifiedResultEntity <bool> result = new UnifiedResultEntity <bool>(); result.msgbox = message; CancellationTokenSource _tokenSource = new CancellationTokenSource(); cancellationToken = _tokenSource.Token; _tokenSource.Cancel(); HttpResponseMessage response = new HttpResponseMessage(); response = request.CreateResponse(HttpStatusCode.Unauthorized); response.Content = new StringContent(Tools.JsonHelper.ToJson(result), Encoding.GetEncoding("UTF-8"), "application/json"); return(base.SendAsync(request, cancellationToken).ContinueWith(task => { return response; })); }
/// <summary> /// 附件上传 /// </summary> /// <param name="fileData">/uploads/avatar/ 结尾必须有“/”</param> /// <param name="ServerPath">保存的文件地址相对路径</param> /// <param name="IsThumb">是否生成缩略图</param> /// <returns></returns> public UnifiedResultEntity <String> Upload(HttpPostedFileBase fileData, string ServerPath, bool IsThumb = true) { UnifiedResultEntity <string> response_entity = new UnifiedResultEntity <string>(); response_entity.msg = 0; if (fileData == null) { response_entity.msgbox = "请选择要上传的文件"; return(response_entity); } //保存的目录 string filePath = IOHelper.GetMapPath(ServerPath); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } try { //int file_size = fileData.ContentLength; //获取文件大小 KB string fileExt = IOHelper.GetFileExt(fileData.FileName).ToLower(); //文件扩展名,不含“.” //if (!CheckFileExt("*." + fileExt, file_type)) //{ // response_entity.msgbox = "非法的文件格式"; // return response_entity; //} //if (!CheckFileSize(fileExt, file_size,file_type)) //{ // response_entity.msgbox = "文件大小超出限制"; // return response_entity; //} string guid = Guid.NewGuid().ToString(); string saveName = guid + "." + fileExt; // 保存文件名称 fileData.SaveAs(filePath + saveName); //if (file_type == SiteEnums.UploadType.image) //{ //如果是上传图片 //if (file_size > SiteConfig.ImgSize * 1024) //{ // //如果超出了限制,则进行压缩 // ImgThumbnail ya = new ImgThumbnail(); // //获取图片的宽高 // Hashtable hash =IOHelper.GetImageWidthHeight(filePath + saveName); // //临时文件名 // string saveTempName = guid+"_temp."+fileExt; // if (ya.Thumbnail(filePath + saveName, filePath + saveTempName, TypeHelper.ObjectToInt(hash["w"]), TypeHelper.ObjectToInt(hash["h"]), 20, ImgThumbnail.ImgThumbnailType.H)) // { // //压缩成功,删掉大文件 // IOHelper.DeleteIOFile(filePath + saveName); // saveName = saveTempName;//把文件指向新压缩的文件 // } // else // { // response_entity.msgbox = "图片太大,压缩失败"; // return response_entity; // } //} //} //获取MD5值 string md5 = IOHelper.GetMD5HashFromFile(filePath + saveName); if (System.IO.File.Exists(filePath + md5 + "." + fileExt)) { System.IO.File.Delete(filePath + saveName); //把刚刚上传的给删掉,只用原有的文件 } else //不存在,改名为md5值保存 { System.IO.File.Move(filePath + saveName, filePath + md5 + "." + fileExt); //给文件改名 } //if (IsThumb) //{ // string[] img_arr = { "jpg", "jpeg", "bmp", "png" }; // if (img_arr.Contains(fileExt)) // { // //如果是图片,就压缩 // IOHelper.GenerateThumb_ME(IOHelper.GetMapPath(ServerPath + md5 + "." + fileExt)); // } //} response_entity.msg = 1; response_entity.msgbox = "上传成功"; response_entity.data = ServerPath + md5 + "." + fileExt; return(response_entity); } catch (Exception ex) { response_entity.msgbox = ex.Message; return(response_entity); } }
/// <summary> /// 压缩包上传 /// </summary> /// <param name="fileData"></param> /// <param name="ServerPath"></param> /// <returns></returns> public UnifiedResultEntity <string> Upload_Zip(HttpPostedFileBase fileData, string ServerPath) { UnifiedResultEntity <string> response_entity = new UnifiedResultEntity <string>(); response_entity.msg = 0; if (fileData == null) { response_entity.msgbox = "请选择要上传的文件"; return(response_entity); } //保存的目录 string filePath = IOHelper.GetMapPath(ServerPath); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } try { int file_size = fileData.ContentLength; //获取文件大小 KB string fileExt = IOHelper.GetFileExt(fileData.FileName); //文件扩展名,不含“.” //if (!CheckFileExt("*." + fileExt, SiteEnums.UploadType.zip)) //{ // response_entity.msgbox = "只允上传*.zip格式的文件"; // return response_entity; //} //if (!CheckFileSize(fileExt, file_size, SiteEnums.UploadType.zip)) //{ // response_entity.msgbox = "文件大小超出限制"; // return response_entity; //} string saveName = Guid.NewGuid().ToString() + "." + fileExt; // 保存文件名称 fileData.SaveAs(filePath + saveName); string md5 = IOHelper.GetMD5HashFromFile(filePath + saveName); if (Directory.Exists(filePath + md5 + "/")) { System.IO.File.Delete(filePath + saveName); //把刚刚上传的给删掉,使用原来解压的文件 } else //不存在,改名为md5值保存 { Directory.CreateDirectory(filePath + md5 + "/"); //创建解压后保存的目录 System.IO.File.Move(filePath + saveName, filePath + md5 + "." + fileExt); //给文件改名 try { //解压操作 string glassDir = filePath; FastZip fastZip = new FastZip(); fastZip.ExtractZip(filePath + md5 + "." + fileExt, filePath + md5 + "/", string.Empty); } catch { //删除刚刚创建的目录 Directory.Delete(filePath + md5 + "/"); response_entity.msgbox = "解压失败"; return(response_entity); } } System.IO.File.Delete(filePath + md5 + "." + fileExt); //把刚刚上传的ZIP文件给删掉 response_entity.msg = 1; response_entity.msgbox = "上传成功"; response_entity.data = ServerPath + md5 + "/"; return(response_entity); } catch (Exception ex) { response_entity.msgbox = ex.Message; return(response_entity); } }