2

Amazon S3バケットからリモートイメージをロードして、バイナリでブラウザーに送信しようとしています。同時にASP.Netも学ぼうとしています。私は何年もの間古典的なプログラマーであり、変える必要があります。私は昨日始めて、今日最初の頭痛がします。

私のアプリケーションのページには、次の画像要素があります。

<img src="loadImage.ashx?p=rqrewrwr">

そしてloadImage.ashxに、私はこの正確なコードを持っています:

-------------------------------------------------
<%@ WebHandler Language="C#" Class="Handler" %>

string url = "https://............10000.JPG";
byte[] imageData;
using (WebClient client = new WebClient()) {
   imageData = client.DownloadData(url);
}

public void ProcessRequest(HttpContext context)
{
    context.Response.OutputStream.Write(imageData, 0, imageData.Length);
}
-------------------------------------------------

これは.netでの最初の試みであり、私が何をしているのかわからないため、おそらくこれにはかなりの間違いがあります。まず、次のエラーが発生しますが、まだまだあります。

CS0116: A namespace does not directly contain members such as fields or methods

これは3行目です。string url = "https://............"

4

1 に答える 1

5

HttpHandlerの場合、コードをコードビハインドに配置する必要があります...ソリューションエクスプローラーでloadimage.ashxを展開すると、loadimage.ashx.csファイルが表示されます。このファイルはロジックが存在する場所であり、すべてがProcessRequestメソッドに存在する必要があります。

したがって、loadimage.ashxは基本的に空である必要があります。

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

そして、loadimage.ashx.csには残りが含まれている必要があります。

using System.Web;

public class loadimage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string url = "https://............10000.JPG";
        byte[] imageData;
        using (WebClient client = new WebClient())
        {
            imageData = client.DownloadData(url);
        }

        context.Response.OutputStream.Write(imageData, 0, imageData.Length);
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

または、画像を提供するaspxページを作成することもできます。これにより、コードビハインド要件が削除されますが、オーバーヘッドが少し増えます...次のようにloadimage.aspxページを作成します。

<%@ Page Language="C#" AutoEventWireup="true" %>

<script language="c#" runat="server">
    public void Page_Load(object sender, EventArgs e)
    {
        string url = "https://............10000.JPG";
        byte[] imageData;
        using (System.Net.WebClient client = new System.Net.WebClient())
        {
            imageData = client.DownloadData(url);
        }

        Response.ContentType = "image/png";  // Change the content type if necessary
        Response.OutputStream.Write(imageData, 0, imageData.Length);
        Response.Flush();
        Response.End();
    }
</script>

次に、ashxではなくimagesrcでこのloadimage.aspxを参照します。

于 2012-07-22T03:17:31.917 に答える