ASP.NET一般处理程序读取数据库图片失败,求技术帮助
排查ASP.NET一般处理程序无法读取数据库图片的问题
先从代码里的致命问题入手,一步步排查修复:
1. 移除空异常捕获,暴露错误细节
原代码的catch {}直接吞掉所有异常,你根本不知道哪里出了问题。改成捕获并输出异常信息,这样访问处理程序时能直接看到错误:
catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write($"错误信息: {ex.Message}\n堆栈跟踪: {ex.StackTrace}"); }
2. 修正MemoryStream的生命周期问题
原代码在创建Image对象后立刻关闭了流,但Image依赖该流才能正常工作。改用using语句自动管理流的生命周期:
// 替换原有的ms声明和初始化 using (MemoryStream ms = new MemoryStream(img)) { Image icon = Image.FromStream(ms); // 所有使用icon的逻辑都放在这个using块内 }
3. 增加参数与数据有效性检查
- 检查
id参数是否合法,避免转换失败:if (!int.TryParse(context.Request.QueryString["id"], out int id)) { context.Response.ContentType = "text/plain"; context.Response.Write("缺少或无效的id参数"); return; } - 验证数据库返回的图片数据是否存在:
PsbE = Psb.GetInfo(id); if (PsbE == null || PsbE.Psblcon == null || PsbE.Psblcon.Length == 0) { context.Response.ContentType = "text/plain"; context.Response.Write("未找到对应图片数据"); return; } - 处理可能为空的
mode和module参数:string mode = request.Params["mode"] ?? "0"; // 给默认值 string module = request.QueryString["module"]; if (string.IsNullOrEmpty(module)) { context.Response.ContentType = "text/plain"; context.Response.Write("缺少module参数"); return; }
4. 修正ContentType初始设置
原代码开头把ContentType设为text/plain,会导致正常返回图片时可能出现类型冲突。可以先默认设为图片类型,出错时再切换为文本:
// 开头替换原ContentType设置 context.Response.ContentType = "image/jpeg";
5. 清理冗余代码
原代码中创建的WEIP.Sys.Attachment attach对象未被使用,直接删除即可。
修正后的完整代码示例
public class StffIcon : IHttpHandler { ProductStaffBasic Psb = new ProductStaffBasic(); ProductStaffBasicEntity PsbE = new ProductStaffBasicEntity(); public void ProcessRequest(HttpContext context) { try { context.Response.ContentType = "image/jpeg"; HttpRequest request = context.Request; // 检查id参数 if (!int.TryParse(context.Request.QueryString["id"], out int id)) { context.Response.ContentType = "text/plain"; context.Response.Write("缺少或无效的id参数"); return; } // 获取数据库数据 PsbE = Psb.GetInfo(id); if (PsbE == null || PsbE.Psblcon == null || PsbE.Psblcon.Length == 0) { context.Response.ContentType = "text/plain"; context.Response.Write("未找到对应图片数据"); return; } // 处理图片流 using (MemoryStream ms = new MemoryStream(PsbE.Psblcon)) { Image icon = Image.FromStream(ms); int thumbnailWidth = TypeParse.StrToInt(request["tw"], -1); int thumbnailHeight = TypeParse.StrToInt(request["th"], -1); string mode = request.Params["mode"] ?? "0"; string module = request.QueryString["module"]; if (string.IsNullOrEmpty(module)) { context.Response.ContentType = "text/plain"; context.Response.Write("缺少module参数"); return; } WEIP.Site.Thumbnail.BgColor color = WEIP.Site.Thumbnail.BgColor.White; if (mode == "0") { color = WEIP.Site.Thumbnail.BgColor.White; } if (thumbnailWidth == -1 && thumbnailHeight == -1) { ImageFormat thisFormat = icon.RawFormat; if (thisFormat.Equals(ImageFormat.Gif)) { context.Response.ContentType = "image/gif"; } else { context.Response.ContentType = "image/jpeg"; } using (Bitmap outBmp = new Bitmap(icon)) { outBmp.Save(context.Response.OutputStream, thisFormat); } } else { new WEIP.Site.Thumbnail().GetSmallImage(context.Response, icon, thumbnailWidth, thumbnailHeight, color); } } } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write($"错误信息: {ex.Message}\n堆栈跟踪: {ex.StackTrace}"); } } public bool IsReusable { get { return false; } } }
内容的提问来源于stack exchange,提问作者Toum Kall
相关产品推荐
相关产品推荐

