【问题标题】:read image using ashx doesn't work in asp.net使用 ashx 读取图像在 asp.net 中不起作用
【发布时间】:2015-06-21 06:50:09
【问题描述】:

我正在尝试使用 ashx 从数据库加载图像。

当我使用此代码时,图像已成功加载

 <% <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Id", "~/Handler/CategoryHandler.ashx?catId={0}") %>' />

但是当我使用这段代码时

   <%
            foreach (ProductCategories VARIABLE in categoriesList)
            {
                 Response.Write("<div class='wrapper-box'>" +
                                    "<a href='product/product.aspx'>" +
                                        "<img src='~/Handler/ProductHandler.ashx?Id="+VARIABLE.Id+"'/>" +
                                        "<p>"+VARIABLE.CategoryName+"</p>" +
                                    "</a>" +
                                "</div>");
            }

             %>

图片无法加载。为什么代码不起作用?

ashx文件是这样的:

 public void ProcessRequest(HttpContext context)
    {
        // Set up the response settings
        context.Response.ContentType = "image/jpeg";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput = false;
        // Setup the Size Parameter
        ImageSize size;
        switch (context.Request.QueryString["Size"])
        {
            case "S":
                size = ImageSize.Small;
                break;
            case "L":
                size = ImageSize.Large;
                break;
            default:
                size = ImageSize.Small;
                break;
        }
        // Setup the PhotoID Parameter
        Stream stream;
        if (context.Request.QueryString["Id"] != null && context.Request.QueryString["Id"] != "")
        {
            Int32 id = Convert.ToInt32(context.Request.QueryString["Id"]);
            stream = Products.GetImageStream(id, size);
            //context.Response.AddHeader("content-disposition", String.Format("attachement;filename=\"{0}\"", );
            // Get the photo from the database, if nothing is returned, get the default "placeholder" photo
            if (stream == null) return;
            // Write image stream to the response stream
            const int buffersize = 1024 * 16;
            byte[] buffer = new byte[buffersize];
            int count = stream.Read(buffer, 0, buffersize);
            while (count > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, count);
                count = stream.Read(buffer, 0, buffersize);
            }
        }
    }

【问题讨论】:

    标签: c# asp.net image ashx


    【解决方案1】:

    问题是Response.Write没有将~字符扩展为基本URL,所以在页面的HTML中生成的图片的URL是这样的:

    <img src='~/Handler/ProductHandler.ashx?Id=123' />
    

    为了解决这个问题,你必须在Response.Write中使用之前扩展URL:

    <%
        foreach (ProductCategories VARIABLE in categoriesList)
        {
            var imgUrl = ResolveUrl("~/Handler/ProductHandler.ashx?Id=" + VARIABLE.Id.ToString());
            Response.Write("<div class='wrapper-box'>" +
                "<a href='product/product.aspx'>" +
                    "<img src='" + imgUrl + "'/>" +
                    "<p>"+VARIABLE.CategoryName+"</p>" +
                "</a>" +
            "</div>");
    }
    %> 
    

    【讨论】:

    • 图片保存在数据库的Image字段中
    • 图片无法加载!
    • @EhsanAkbar:什么不起作用?你有错误吗?我注意到您在示例中使用了两个不同的处理程序。 ProductHandler.ashx 有没有用过?能否在浏览器中通过有效的 URL 成功调用,从而显示对应的图片?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多