我有一个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,对它的工作原理一无所知,请尽可能简单地解释,谢谢。
img src
。稍后我打算让HttpHandler生成缩略图,然后返回其路径。 - Daniel