How to use .ashx handler with asp: Image object?
I have an ashx handler:
<%@ 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; }
}
}
Now all this handler receives the image and returns it. On my aspx page, I have this line:
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />
And the C # code behind it:
Image1.ImageUrl = "Thumbnail.ashx?image=../Files/random guid string/test.jpg";
When I browse the webpage, the images are not displayed, and what I typed is displayed in HTML:
<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />
Can someone tell me why this is not working? Unfortunately, I just started working with ASP.NET yesterday, and I don’t know how it works, so please keep the explanations simple if possible, thanks.
. .
. http://onlineshoping.somee.com/Fimageview.ashx?FImageName=FrozenToront.Jpg
.ashx ASPX html.
:
<image src="/Timageview.ashx?TImageName=FrozenToront.Jpg"
width="100" height="75" border="1"/>
<%@
WebHandler Language="C#"
Class="Fimageview"
%>
/// <summary>
/// Created By Rajib Chowdhury Mob. 01766-306306; Web: http://www.rajibs.tk
/// Email: mysuccessstairs@hotmail.com
/// FB: https://www.facebook.com/fencefunny
///
/// Complete This Page Coding On Fabruary 12, 2014
/// Programing C# By Visual Studio 2013 For Web
/// Dot Net Version 4.5
/// Database Virsion MSSQL Server 2005
/// </summary>
using System;
using System.IO;
using System.Web;
public class Fimageview : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "image/png";
string file = context.Request.QueryString["FImageName"];
if (string.IsNullOrEmpty(file))
{
context.Response.Redirect("~/default");//If RequestQueryString Null Or Empty
}
try
{
if (file == "")
{
context.Response.Redirect("~/Error/PageNotFound");
}
else
{
r.WriteFile("/Catalog/Images/" + file); //Image Path If File Name Found
}
}
catch (Exception)
{
context.Response.ContentType = "image/png";
r.WriteFile("/yourImagePath/NoImage.png");//Image Path If File Name Not Found
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
, , , . , , :
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" />
<asp:Image ID="Image1" runat="server" CssClass="thumbnail" imageurl='<%# "~/Thumbnail.ashx?image=" + Utilities.ToURLEncoding("~\\Files" + DataBinder.Eval(Container.DataItem, "file_path")) %>' />
HTML, % 5C .., .
Oh, I forgot to mention that it is in a DataList. Using Image1.ImageUrl = "Thumbnail.ashx?image=blahblah";outside the DataList works great, oddly enough.