/// <summary> /// 下载文件 /// </summary> /// <param name="path"></param> /// <returns>{ /// code:'success-成功,failed-失败', /// msg:'提示信息', /// filetype:'文件的content-type类型' /// filename:'文件名称', /// filelength:'文件长度', /// file:'文件内容,采用base64加密' /// }</returns> public object DoDownLoad(string path) { path = path.Replace("~", upload_root_path); if (!File.Exists(path)) { return(new { code = "failed", msg = "文件不存在" }); } var filecontent = File.ReadAllBytes(path); var width = ""; var height = ""; if (new string[] { "jpg", "png", "bmp", "gif", "jpeg" }.Contains(Path.GetExtension(path).Replace(".", "").ToLower())) { using (System.Drawing.Image image = System.Drawing.Image.FromFile(path)) { width = image.Width.ToString(); height = image.Height.ToString(); } } return(new { code = "success", msg = "", filetype = ResponseHeader_ContentType.Map(Path.GetExtension(path).ToLower().Replace(".", "")), filename = Path.GetFileName(path), filelength = filecontent.Count(), file = ComFunc.Base64Code(filecontent), pic_width = width, pic_heigth = height }); }
object ExportReport(string id) { SetCacheEnable(false); dynamic result = FrameDLRObject.CreateInstance(QueryReport(id)); if (result.code != "success") { return(result); } var columns = ((IEnumerable <object>)result.columns).Select(d => (FrameDLRObject)FrameDLRObject.CreateInstance(d)).ToList(); var data = ((IEnumerable <object>)result.data).Select(d => (FrameDLRObject)FrameDLRObject.CreateInstance(d)).ToList(); var file = GenerateExcel(columns, data); return(new { code = "success", msg = "", filetype = ResponseHeader_ContentType.Map("xlsx"), filename = result.report_name, filelength = file.Length, file = ComFunc.Base64Code(file) }); }
/// <summary> /// 静态文件资源请求处理,可扩展 /// </summary> /// <param name="context"></param> /// <param name="requestExtType"></param> protected virtual void ProcessStaticFile(HttpContext context, string requestExtType) { IFileInfo fileInfo = _middleware_options.GetStaticFileInfo(_hostenv.ContentRootPath, context, requestExtType); if (fileInfo.Exists) { if (_middleware_options.IsOpenStaticFileType(context, requestExtType)) { context.Response.StatusCode = 200; context.Response.ContentType = ResponseHeader_ContentType.Map(requestExtType); context.Response.Headers.Add("Content-Length", fileInfo.Length + ""); //var bytes = ComFunc.StreamToBytes(fileInfo.CreateReadStream()); //context.Response.Body.Write(bytes, 0, bytes.Length); context.Response.SendFileAsync(fileInfo).Wait(); } else { context.Response.StatusCode = 403; if (string.IsNullOrEmpty(_middleware_options.PagePath4Forbidden)) { context.Response.Headers.Add("Content-Length", "0"); } else { var path = _middleware_options.PagePath4Forbidden.Replace("~", _hostenv.ContentRootPath); if (File.Exists(path)) { var resultmsg = File.ReadAllText(path); var msgbytelength = Encoding.UTF8.GetByteCount(resultmsg); context.Response.ContentType = ResponseHeader_ContentType.html + ";charset=utf-8"; context.Response.Headers.Add("Content-Length", msgbytelength + ""); context.Response.WriteAsync(resultmsg).Wait(); } else { context.Response.Headers.Add("Content-Length", "0"); } } } } else { context.Response.StatusCode = 404; if (string.IsNullOrEmpty(_middleware_options.PagePath4NotFound)) { context.Response.Headers.Add("Content-Length", "0"); } else { var path = _middleware_options.PagePath4NotFound.Replace("~", _hostenv.ContentRootPath); if (File.Exists(path)) { var resultmsg = File.ReadAllText(path); var msgbytelength = Encoding.UTF8.GetByteCount(resultmsg); context.Response.ContentType = ResponseHeader_ContentType.html + ";charset=utf-8"; context.Response.Headers.Add("Content-Length", msgbytelength + ""); context.Response.WriteAsync(resultmsg).Wait(); } else { context.Response.Headers.Add("Content-Length", "0"); } } } context.Response.Body.Flush(); }
protected override void OnError(Exception ex, ParameterStd p, DataCollection d) { var ep = (EWRAParameter)p; var ed = (EWRAData)d; ed.StatusCode = Constants.RestStatusCode.INVALID_REQUEST; var isdebug = p[DomainKey.CONFIG, "DebugMode"] == null ? false : (bool)p[DomainKey.CONFIG, "DebugMode"]; string errorCode = "E-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); string errlog = ""; if (ex is HostJsException) { var jex = (HostJsException)ex; if (ex.InnerException != null) { if (ex.InnerException is HostJsException) { var ijex = (HostJsException)ex.InnerException; errlog = string.Format("错误编号:{0},\n{1}\n{2}\n出错代码行数{3}\n出错代码列数{4}\n出错代码位置{5}\nInnerException:{6}\n{7}\n出错代码行数{8}\n出错代码列数{9}\n出错代码位置{10}", errorCode, ex.Message, ex.StackTrace, jex.Line, jex.Column, jex.SourceCode.Replace("\"", "'"), ex.InnerException.Message, ex.InnerException.StackTrace, ijex.Line, ijex.Column, ijex.SourceCode.Replace("\"", "'")); } else { errlog = string.Format("错误编号:{0},\n{1}\n{2}\n出错代码行数{3}\n出错代码列数{4}\n出错代码位置{5}\nInnerException:{6}\n{7}", errorCode, ex.Message, ex.StackTrace, jex.Line, jex.Column, jex.SourceCode, ex.InnerException.Message, ex.InnerException.StackTrace); } } else { errlog = string.Format("错误编号:{0},\n{1}\n{2}\n出错代码行数{3}\n出错代码列数{4}\n出错代码位置{5}", errorCode, ex.Message, ex.StackTrace, jex.Line, jex.Column, jex.SourceCode.Replace("\"", "'")); } } else { if (ex.InnerException != null) { if (ex.InnerException is HostJsException) { var ijex = (HostJsException)ex.InnerException; errlog = string.Format("错误编号:{0},\n{1}\n{2}\nInnerException:{3}\n{4}\n\n出错代码行数{5}\n出错代码列数{6}\n出错代码位置{7}", errorCode, ex.Message, ex.StackTrace, ex.InnerException.Message, ex.InnerException.StackTrace, ijex.Line, ijex.Column, ijex.SourceCode.Replace("\"", "'")); } else { errlog = string.Format("错误编号:{0},\n{1}\n{2}\nInnerException:{3}\n{4}", errorCode, ex.Message, ex.StackTrace, ex.InnerException.Message, ex.InnerException.StackTrace); } } else { errlog = string.Format("错误编号:{0},\n{1}\n{2}", errorCode, ex.Message, ex.StackTrace); } } GlobalCommon.Logger.WriteLog(LoggerLevel.ERROR, errlog); var contentbody = FrameDLRObject.CreateInstance(FrameDLRFlags.SensitiveCase); var errormsg = ""; if (isdebug) { errormsg = string.Format("出错了,{0}", errlog);; } else { errormsg = string.Format("系统错误!!!(错误编号:{0})", errorCode); } contentbody.error = errormsg; var jsonmsg = ((FrameDLRObject)contentbody).ToJSONString(true); var msgbytelength = Encoding.UTF8.GetByteCount(jsonmsg); SetHeaders(ep, ed); if (page_500 == "") { ep.CurrentHttpContext.Response.StatusCode = (int)ed.StatusCode; ep.CurrentHttpContext.Response.ContentType = ResponseHeader_ContentType.Map(ComFunc.Enum2String <RestContentType>(ed.ContentType).ToLower()) + ";charset=utf-8"; ep.CurrentHttpContext.Response.Headers.Add("Content-Length", msgbytelength + ""); ep.CurrentHttpContext.Response.WriteAsync(jsonmsg); ep.CurrentHttpContext.Response.Body.FlushAsync(); } else { ResponsePageContent(page_500, ep, ed); } }