2012-02-01 7 views
1

내 프로젝트에서는 ajax() jquery 함수로 비동기 호출로 캐시 된 이미지를 동적으로 표시하고 싶습니다.ajax() jquery 함수의 비동기 호출로 캐시에서 이미지 표시

$.ajax({ 
    type: “POST”, 
    url: “mypage.aspx/GetThumbnail”, 
    data: “{‘id’:'" + idThumbnail + "’}”, 
    contentType: “application/json; charset=utf-8?, 
    dataType: “json”, 
    success: function(msg) { 
     // Hide the fake progress indicator graphic. 
     $('#thumbnailContent').removeClass('loading'); 

     // Insert the returned HTML into the <div>. 
     $('#thumbnailContent').html(msg.d); 
    } 
}); 

내의 WebMethod의 반환이 같은 HTML로 표시 할 수 있도록 성공 인 경우 비동기 적 정적의 WebMethod "mypage.aspx"페이지 "의 getThumbnail를"호출이 JQuery와 블록 코드 :

<img url="myThumbnailImage.jpg" /> 

내 문제는 문자열에 HTML 서식을 반환해야 정적의 WebMethod에서 온,하지만 난 캐시에서 데이터를 읽을 때 나는 []과 같이 바이트를 복구 :

if (HttpContext.Current.Cache[id_thumbnail+ "_thumbnail"] != null) 
    { 
    byte[] mytemptabbyte = (byte[])HttpContext.Current.Cache[id_thumbnail + "_thumbnail"]; 
    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.AddHeader("Content-Type", "image/jpeg"); 
    HttpContext.Current.Response.Write(""); 
    HttpContext.Current.Response.BinaryWrite(mytemptabbyte); 
    HttpContext.Current.Response.Flush(); 
    HttpContext.Current.Response.End(); 
    } 

에게 retu을 할 수있는 방법 내 캐시 된 이미지에 대한 html 형식의 응답?

편집 :

그래서 내가 같이 할 수있는 새로운 aspx 페이지를 호출?

function LoadThumnail(id_thumbnail){ 
    $.ajax({ 
    type: “POST”, 
    url: “getThumbnail.aspx?id_thumbnail=”id_thumbnail, 
    data: “{}”, 
    contentType: “application/json; charset=utf-8?, 
    dataType: “json”, 
    success: function(msg) { 

     // Insert the returned HTML into the <div>. 
     $('#thumbnailContent').html('<img src="' + msg.d + '" />); 

     // Hide the fake progress indicator graphic. 
     $('#thumbnailContent').removeClass('loading'); 
    } 
}); 

}

답변

0

페이지 만들기 (예를 들어 getimage.aspx), 그리고 당신이하는 OnInit/후 Page_Init 해당 페이지의에 "캐시에서 이미지를 받고"라는 코드를 붙여 넣으면됩니다.

<img url="getimage.aspx?id=myThumbnailImage.jpg" /> 

또는

<img url="getimage.aspx?id=1234" /> 

그런 다음, 또한 Request.QueryString["id"]을 읽고 캐시에서 이미지를 가져 오지 그것을 사용 : 다음

의 IMG 태그 이런 식으로 뭔가 할 수 있습니다.

+0

좋아요.하지만 이렇게하면 비동기 호출이 아니며 요청시 미리보기 이미지를로드하고 싶습니다 ... – bor1s

+0

@ bor1s는 아약스에 의해 언급 된 페이지를 비동기 적으로 호출합니다. 아약스 요청은 항상 문자열 표현을 가져옵니다. – Igarioshka

+0

사과와 배, 여러분. 그는 그의 ajax 호출이 img 태그 html로 간다고 말했다. 그 이미지가 존재하지 않기 때문에 메모리에있는 이미지 데이터를 활용할 수 있도록 마크 업을 변경하는 것이 좋습니다. 다른 방법으로는 이미지 데이터를 디스크에 저장 한 다음 정리에 대해 걱정할 필요없이 걱정할 필요가 없습니다. –

관련 문제