zoukankan      html  css  js  c++  java
  • 用Generic Handler(ashx)去显示非二进制的图片

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

    <img alt="" src="xxx.jpg" />

    <asp:Image ID="Image1" runat="server" ImageUrl="xxx.jpg" />

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

    你可以参考下面代码:

    View Code
    <%@ WebHandler Language="C#" Class="ViewImage" %>

    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;
  • 相关阅读:
    JS中的逻辑或||逻辑与&&
    for 循环里面事件函数的i值
    getByClass--js
    ul 宽度不固定居中
    style.top style.left js
    trigger() 触发事件
    JavaScript中hasOwnProperty函数
    使用windows powershell ISE管理命令窗口,并集成git命令
    nodeJs跨域设置(express,koa2,eggJs)
    node获取本机动态IP,并对应修改相关JavaScript文件的IP地址
  • 原文地址:https://www.cnblogs.com/insus/p/2003740.html
Copyright © 2011-2022 走看看