![](/img/trans.png)
[英]Can I render html from ASP.NET Page objects outside ASP.NET applications?
[英]Using a handler to render an image from a blob on an ASP.NET page. I can't get it
我正在开发一个简单的图像标记和搜索应用程序。 我已经将图像上传到数据库,并且已应用标签,但是当我将其拉回时失败了-图像无法渲染。
我在SO上找到了这个 ,但无法正常工作。
我想我可能是误会了处理程序。
简而言之,在后面的代码中,我将创建一个ASP:Image,将其imageurl设置为带有照片ID的处理程序,然后将该控件添加到ASP:Placeholder中。
当页面呈现时,我在IE中得到那个红色的小x没有图像的东西,而在FF中,则什么也没有。
让我想到我缺少某些东西的一件事是,我的处理程序代码中的断点从未命中。 因此,它甚至正在执行。 对?
有人知道我在做什么错吗? 谢谢。
这是我的经理
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
以及我将其粘贴在占位符中的位置
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
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;
}
}
}
基本上,只要先做一个简单的测试,如果可行,那么我建议这是您从数据库返回的数据。
好奇:您是否正在使用BLOB来避免热链接? (有更好的方法可以做到这一点)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.