Для моего проекта я хотел бы динамически отображать кэшированное изображение с помощью асинхронного вызова функцией jquery ajax ().
$.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);
}
});
Этот код блока JQuery будет асинхронно вызывать статический веб-метод «GetThumbnail» на странице «mypage.aspx», если результат будет успешным, поэтому возвращение моего веб-метода будет отображаться в виде html, например:
<img url="myThumbnailImage.jpg" />
Моя проблема возникла из-за статического веб-метода, который должен возвращать форматированный HTML-код в строке, но когда я читаю данные из кэша, я восстанавливаю байт [] следующим образом:
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();
}
Как вернуть ответ в формате 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');
}
});
}