Отображение изображения ashx с помощью jQuery?
Я пытался использовать плагин jQuery colorbox в для отображения изображений, которые у меня есть в моей БД через файл ashx. К сожалению, он просто выплевывает кучу тарабарщины в верхней части страницы и никакого изображения. Можно ли это сделать? Вот что у меня есть до сих пор:
$(document).ready
(
function ()
{
$("a[rel='cbImg']").colorbox();
}
);
...
<a rel="cbImg" href="HuntImage.ashx?id=15">Click to see image</a>
обновление:
мой файл ashx записывает двоичный файл:
context.Response.ContentType = "image/bmp";
context.Response.BinaryWrite(ba);
4 ответов
Colorbox имеет опцию "фото". Если вы установите значение true в конструкторе, то это заставит его чтобы сделать фото.
$(target).colorbox({photo: true});
вы должны установить атрибут src на стороне клиента.
<img src="HuntImage.ashx?id=15" ..../>
проводник
public class ImageRequestHandler: IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if(context.Request.QueryString.Count != 0)
{
//Get the stored image and write in the response.
var storedImage = context.Session[_Default.STORED_IMAGE] as byte[];
if (storedImage != null)
{
Image image = GetImage(storedImage);
if (image != null)
{
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
private Image GetImage(byte[] storedImage)
{
var stream = new MemoryStream(storedImage);
return Image.FromStream(stream);
}
public bool IsReusable
{
get { return false; }
}
}
похоже, что я не могу сделать то, что я пытаюсь использовать colorbox с изображением ashx. Если кто-нибудь найдет способ, пожалуйста, разместите его здесь.
Я думал удалить вопрос, но я оставлю его, если кто-то другой столкнется с той же проблемой.
найдите эту функцию вокруг строки 124 (colorbox 1.3.15)
// Checks an href to see if it is a photo.
// There is a force photo option (photo: true) for hrefs that cannot be matched by this regex.
function isImage(url) {
return settings.photo || /\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
}
в строке 127, добавить |ashx
после bmp в (gif|png|jpg|jpeg|bmp)
Итак, это звучит так:
// Checks an href to see if it is a photo.
// There is a force photo option (photo: true) for hrefs that cannot be matched by this regex.
function isImage(url) {
return settings.photo || /\.(gif|png|jpg|jpeg|bmp|ashx)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
}
это работает просто отлично для меня в Sitecore 6.2:)