3

私は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";

Webページを表示すると、画像が表示されず、HTMLには入力した内容が正確に表示されます。

<img class="thumbnail" src="Thumbnail.ashx?image=../Files%5Crandom guid string%5Cimages%5Ctest.jpg" style="border-width:0px;" />

なぜこれが機能しないのか誰かに教えてもらえますか?残念ながら、私は昨日ASP.NETを使い始めたばかりで、どのように機能するのかわかりません。可能な限り、説明を簡潔にしてください。ありがとうございます。

4

3 に答える 3

5

実際の画像の内容ではなく、画像へのパスを印刷しています。使用する

context.Response.WriteFile(context.Server.MapPath(imagePath));

代わりにメソッド。

パスを安全な場所に制限してください。そうしないと、ユーザーがサーバー上の任意のファイルの内容を表示できるため、セキュリティの脆弱性が発生する可能性があります。

于 2009-07-08T04:34:47.710 に答える
2

このコードは、指定されたパスからファイル名で画像を表示するのに役立ちます。
元。http://onlineshoping.somee.com/Fimageview.ashx?FImageName=FrozenToront.Jpg

ASPX html 本文で .ashx を使用できます。

例:

<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;
        }
    }
}
于 2014-02-12T15:22:35.640 に答える
0

私は元のポスターであり、最終的に問題を理解しました。私がしなければならなかったのは、変更することだけでした:

<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 などになります。そうしないと機能しません。

ああ、言い忘れましたが、これは DataList にあります。Image1.ImageUrl = "Thumbnail.ashx?image=blahblah";奇妙なことに、DataList の外で使用すると、完全に正常に動作します。

于 2009-07-08T23:28:27.620 に答える