ITPub博客

首页 > IT职业 > IT职场 > 用Generic Handler(ashx)去显示非二进制的图片

用Generic Handler(ashx)去显示非二进制的图片

IT职场 作者:m53469 时间:2021-09-09 09:12:30 0 删除 编辑

一般情况,显示非二进制的图片(存放在磁盘上的图片文件),直接用图片控件轻易实现。

图片描述

 

由于程序要求,需要把图片文件转为数据流(二进制),再进行显示。因此想起使用Generic Handler(ashx)来处理。

你可以参考下面代码:

View Code

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

public class ViewImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //接收图片路径
        string parameter = context.Request.QueryString["file"];      
        //使用UrlDecode解编码  
        string path = context.Server.MapPath(HttpUtility.UrlDecode(parameter));
        //转为字节
        byte[] datas = System.IO.File.ReadAllBytes(path);
        //输出数据流
        context.Response.OutputStream.Write(datas, 0, datas.Length);
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

  

xxx.NavigateUrl = "~/ViewImage.ashx?file=" + fileFullPath;

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2819/viewspace-2800583/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-19

  • 博文量
    80
  • 访问量
    13440