2012-07-22 5 views
2

Amazon S3 버킷에서 원격 이미지를로드하여 브라우저에 바이너리로 보내려고합니다. 동시에 ASP.Net도 배우려고합니다. 나는 수년 동안 고전적인 프로그래머 였고 변화해야 할 필요가있다. 나는 어제 시작했고 오늘 내 첫 두통이있다. 나는이 이미지 요소를 내 응용 프로그램에서 페이지 원격 이미지로드 및 .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); 
} 
------------------------------------------------- 

아마 꽤 많은 잘못이있다 :

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

및 loadImage.ashx에, 나는이 정확한 코드가

이것은 넷에서의 첫 번째 시도이기 때문에 내가하는 일을 모른다. 우선, 다음과 같은 오류가 나옵니다.
CS0116: A namespace does not directly contain members such as fields or methods 

, 당신은 당신이 솔루션 탐색기에서 loadimage.ashx을 확장하는 경우 ... 뒤에 코드에 코드를 삽입해야 HttpHandler를 들어 string url = "https://............"

답변

5

입니다 라인 3에, 당신은 볼 수 loadimage.ashx.cs 파일 이 파일은 논리가 있어야하며 모든 파일이 ProcessRequest 메서드에 있어야합니다.

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

그리고 loadimage.ashx.cs 나머지 포함해야합니다 :

그래서 loadimage.ashx은 기본적으로 비어 있어야 또는

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 페이지를 만들 수 있습니다 .

<%@ 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의 이미지 SRC이 loadimage.aspx 참조 :이 다음으로 loadimage.aspx 페이지를 만들 ... 요구 사항 뒤에 코드를 제거하지만 조금 더 오버 헤드를 추가합니다.

+0

VS 또는 솔루션 탐색기를 사용하고 있지 않습니다. Dreamweaver에서 loadImage.ashx.cs 파일을 새로 만들 수 있습니까? – TheCarver

+0

방금 ​​새 페이지 loadImage.ashx.cs를 만들었으며 "Handler '유형을 만들 수 없습니다."라는 새로운 오류가 발생했습니다. – TheCarver

+0

저는 Dreamweaver에 익숙하지 않지만 요청을 처리하는 .cs 파일에 클래스를 만들어야한다고 생각합니다. ashx 파일의'Class' 속성 선언은 완전한 클래스 이름 (네임 스페이스와 클래스 이름)을 가리켜 야합니다. –

관련 문제