简体   繁体   English

使用处理程序从ASP.NET页上的Blob渲染图像。 我不明白

[英]Using a handler to render an image from a blob on an ASP.NET page. I can't get it

I'm working on a simple image tagging and searching app. 我正在开发一个简单的图像标记和搜索应用程序。 I've got my images uploading to the DB, the tags being applied, but am failing when I pull them back - the images don't render. 我已经将图像上传到数据库,并且已应用标签,但是当我将其拉回时失败了-图像无法渲染。

I found this here on SO, but I'm not able to get it working. 我在SO上找到了这个 ,但无法正常工作。

I think I am perhaps misunderstanding handlers. 我想我可能是误会了处理程序。

In short, in the code behind, I'm creating an ASP:Image, setting its imageurl to the handler with the id of the photo, and then adding that control to an ASP:Placeholder. 简而言之,在后面的代码中,我将创建一个ASP:Image,将其imageurl设置为带有照片ID的处理程序,然后将该控件添加到ASP:Placeholder中。

When the page renders, I get, in IE, that little red x no image thing, and in FF, nothing. 当页面呈现时,我在IE中得到那个红色的小x没有图像的东西,而在FF中,则什么也没有。

One thing that gets me thinking I'm missing something is that a breakpoint in my handler code is never hit. 让我想到我缺少某些东西的一件事是,我的处理程序代码中的断点从未命中。 So it's even getting executed. 因此,它甚至正在执行。 Right? 对?

Anyone know what I'm doing wrong here? 有人知道我在做什么错吗? Thanks. 谢谢。

Here's my handler 这是我的经理

Imports aapeClsLib
Imports System.Web
Imports System.Web.Services

Public Class photos
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim img As Byte() = getImage(context.Request.QueryString("ID"))
        context.Response.Clear()
        context.Response.ContentType = "image/jpeg"
        context.Response.BinaryWrite(img)
        context.Response.End()
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property


    Private Function getImage(ByVal id As String) As Byte()
        Dim img As Byte()

        Dim strSql As String = "select ph_photo from photos where ph_id = " & id
        Dim dt As DataTable = sqLiteData.getDataTable(strSql)
        img = CType(dt.Rows(0)(0), Byte())

        Return img


    End Function
End Class

and where I'm sticking it in my placeholder 以及我将其粘贴在占位符中的位置

Private Sub insertPhotos(ByVal dt As DataTable)
     For Each row As DataRow In dt.Rows

         Dim img As New Image
         img.ImageUrl = "photos.ashx?ID=" & row(0)
         PlaceHolder1.Controls.Add(img)

     Next
 End Sub

It looks like you didn't register the handler in web.config and/or the extension in IIS. 看来您没有在web.config和/或IIS中注册该处理程序。 See here and here for more details. 有关更多详细信息,请参见此处此处

EDIT: I see now that you are using .ashx as the extension, so you usually don't need to register it. 编辑:我现在看到您正在使用.ashx作为扩展名,因此您通常不需要注册它。 Now the main clue is the handler registration in web.config. 现在主要的线索是在web.config中注册处理程序。

C# example, but this works fine for me - you might want to add the content length header: C#示例,但这对我来说很好-您可能要添加内容长度标头:

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

using System;
using System.Web;

public class Photo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(System.IO.File.ReadAllBytes("C:\\Test.jpg"));
        context.Response.AddHeader("Content-Length", new System.IO.FileInfo("C:\\Test.jpg").Length.ToString());
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Basically, just do a simple test first, if that works then I'd suggest it's the data you're returning from the database. 基本上,只要先做一个简单的测试,如果可行,那么我建议这是您从数据库返回的数据。

Curious: Are you using BLOB's to avoid hot-linking? 好奇:您是否正在使用BLOB来避免热链接? (There are much better ways to do this) (有更好的方法可以做到这一点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从ASP.NET应用程序外的ASP.NET页面对象中呈现html吗? - Can I render html from ASP.NET Page objects outside ASP.NET applications? 使用ASP.NET中的File Uploader将图像上传到Azure Blob? - Uploading Image to Azure Blob using File Uploader in ASP.NET? 我可以让PHP在asp.net页面上运行吗? - can I get php to run in an asp.net page? 无法从ASP.NET MVC内部写入Blob存储 - Can't write to blob storage from inside ASP.NET MVC 如何将页脚放在asp.net主页的底部。 - How to keep the footer at bottom in asp.net master page.? 如何使用foreach在ASP.NET中呈现数据? - How can I render my data in ASP.NET with foreach? 如何从ASP.Net中的Custom ServerControl在HTML输入中呈现新属性 - How can I render a new property in an HTML Input from a Custom ServerControl in ASP.Net 从Silverlight页面重定向页面时,ASP.NET文本框无法获得焦点 - ASP.NET Text Box doesn't get focus when page is redirected from a Silverlight page ADFS登录页面本地化 - ASP.NET:我似乎无法动态更改页面的语言 - ADFS Login Page Localization - ASP.NET: I can't seem to be able to change language of the page dynamically 我似乎无法显示我在 asp.net 的 ms access 数据库中保存的图像 - I can't seem to display the image that i saved in ms access database in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM