【问题标题】:How do I use an .ashx handler with an asp:Image object?如何将 .ashx 处理程序与 asp:Image 对象一起使用?
【发布时间】:2010-11-08 22:17:10
【问题描述】:

我有一个 ashx 处理程序:

<%@ WebHandler Language="C#" Class="Thumbnail" %>

using System;
using System.Web;

public class Thumbnail : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string imagePath = context.Request.QueryString["image"];

        // split the string on periods and read the last element, this is to ensure we have
        // the right ContentType if the file is named something like "image1.jpg.png"
        string[] imageArray = imagePath.Split('.');

        if (imageArray.Length <= 1)
        {
            throw new HttpException(404, "Invalid photo name.");
        }
        else
        {
            context.Response.ContentType = "image/" + imageArray[imageArray.Length - 1];
            context.Response.Write(imagePath);
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

现在这个处理程序所做的只是获取一张图片并返回它。在我的 aspx 页面中,我有这一行:

<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />

而它背后的C#代码是:

Image1.ImageUrl = "Thumbnail.ashx?image=../Files/random guid string/test.jpg";

当我查看网页时,图像没有显示,而 HTML 显示的正是我输入的内容:

<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />

谁能告诉我为什么这不起作用?不幸的是,我昨天才开始使用 ASP.NET,我不知道它是如何工作的,所以请尽可能保持简单的解释,谢谢。

【问题讨论】:

    标签: c# asp.net image ashx


    【解决方案1】:

    此代码有助于从指定路径按文件名查看图像。
    前任。 http://onlineshoping.somee.com/Fimageview.ashx?FImageName=FrozenToront.Jpg

    您可以在 ASPX html 正文中使用 .ashx。

    示例:

    <image src="/Timageview.ashx?TImageName=FrozenToront.Jpg"
                                          width="100" height="75" border="1"/>
    


        <%@
            WebHandler Language="C#"
            Class="Fimageview"
        %>
    
        /// <summary>
        /// Created By Rajib Chowdhury Mob. 01766-306306; Web: http://www.rajibs.tk
        /// Email: mysuccessstairs@hotmail.com
        /// FB: https://www.facebook.com/fencefunny
        /// 
        /// Complete This Page Coding On Fabruary 12, 2014
        /// Programing C# By Visual Studio 2013 For Web
        /// Dot Net Version 4.5
        /// Database Virsion MSSQL Server 2005
        /// </summary>
    
        using System;
        using System.IO;
        using System.Web;
    
    public class Fimageview : IHttpHandler {
    
        public void ProcessRequest(HttpContext context)
        {
    
            HttpResponse r = context.Response;
            r.ContentType = "image/png";
            string file = context.Request.QueryString["FImageName"];
            if (string.IsNullOrEmpty(file))
            {
                context.Response.Redirect("~/default");//If RequestQueryString Null Or Empty
            }
            try
            {
                if (file == "")
                {
                    context.Response.Redirect("~/Error/PageNotFound");
                }
                else
                {
                    r.WriteFile("/Catalog/Images/" + file); //Image Path If File Name Found
                }
            }
            catch (Exception)
            {
                context.Response.ContentType = "image/png";
                r.WriteFile("/yourImagePath/NoImage.png");//Image Path If File Name Not Found
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    

    【讨论】:

    • 你的例子太好了。这是我见过的最好的解决方案
    【解决方案2】:

    我是原始发帖人,我终于找到了问题所在。我所要做的就是改变:

    <asp:Image ID="Image1" runat="server" CssClass="thumbnail" />
    

    到:

    <asp:Image ID="Image1" runat="server" CssClass="thumbnail" imageurl='<%# "~/Thumbnail.ashx?image=" + Utilities.ToURLEncoding("~\\Files" + DataBinder.Eval(Container.DataItem, "file_path")) %>' />
    

    标点符号需要更改为它的 HTML 等价物,因此反斜杠变为 %5C 等...,否则它将不起作用。

    哦,我忘了说,这是在 DataList 中。奇怪的是,在 DataList 之外使用 Image1.ImageUrl = "Thumbnail.ashx?image=blahblah"; 效果很好。

    【讨论】:

      【解决方案3】:

      您打印的是图像的路径,而不是实际的图像内容。使用

      context.Response.WriteFile(context.Server.MapPath(imagePath));
      

      改为方法。

      确保将路径限制在某个安全位置。否则,您可能会引入安全漏洞,因为用户将能够看到服务器上任何文件的内容。

      【讨论】:

      • 其实我想要的只是路径,因为我想设置img src。稍后我打算让 HttpHandler 生成一个缩略图,然后返回它的路径。
      • 这不是 HTTP 处理程序所做的。浏览器只是将处理程序视为图像文件。它不能改变页面的原始 HTML 响应。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2014-07-23
      相关资源
      最近更新 更多