【问题标题】:.ashx ASP.NET Handler Image not displayed in html img-element.ashx ASP.NET 处理程序图像未显示在 html img 元素中
【发布时间】:2009-10-23 22:47:57
【问题描述】:

我有一个奇怪的问题。

我创建了一个获取位图并返回它的 ASP.NET 处理程序 (AccidentMap.ashx)。

这是处理程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Drawing;

namespace BackOffice.Modules.Insurance_Company
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AccidentMap : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(context.User.Identity.Name);


                InsuranceCompany.InsuranceCompany insuranceCompany = new InsuranceCompany.InsuranceCompany();

                InsuranceCompany.Accident.Map map = insuranceCompany.GetMap(id, user.UserName, user.GetPassword());

                Bitmap bitmap = map.Image;


                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                byte[] bitmapBytes;

                bitmap.Save(stream, bitmap.RawFormat);
                bitmapBytes = stream.ToArray();

                context.Response.ContentType = "image/jpeg";
                context.Response.OutputStream.Write(bitmapBytes, 0, bitmapBytes.Length);
            }
            catch
            {
            }
        }

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

它通过 GetMap 方法检索图像。

如果我在浏览器中调用这个处理程序,它会显示图像:

主页preisvergleich.de/img/internet/browser.JPG 主页preisvergleich.de/img/internet/Property.JPG

显然 ashx-handler 会返回一个图像。

当我尝试在 html 页面中显示图像时,没有显示任何内容。

主页preisvergleich.de/img/internet/html.JPG

这是页面的html:

<html>
<head>
<title>title</title>
</head>
<body>
<img scr="http://localhost:1849/Modules/Insurance%20Company/AccidentMap.ashx?ID=129" />

</body>
</html>

在两种场景中使用的 url 完全相同。

有人知道这种奇怪行为的原因是什么以及如何解决它?

问候

亚历山大

【问题讨论】:

    标签: jpeg image handler ashx src


    【解决方案1】:

    您在 HTML 中使用“img scr”而不是“img src”?

    【讨论】:

    • 嗨,Aric TenEyck,好吧,我承认:这是一个非常愚蠢的错误!我真的很想知道为什么它不起作用;-) 四只眼睛看到的不仅仅是两只眼睛!谢谢你帮助我! :-) 问候亚历山大
    【解决方案2】:

    您可以通过添加命名空间来做到这一点:

    System.Web.SessionState;
    

    像这样使用它:

    public class Handler : IHttpHandler, IRequiresSessionState
    

    【讨论】:

      猜你喜欢
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 2012-10-17
      • 1970-01-01
      • 2021-02-09
      • 2011-03-27
      相关资源
      最近更新 更多