使用jQuery显示ashx图像?

时间:2010-12-30 04:44:18

标签: asp.net jquery colorbox

我一直在尝试使用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 个答案:

答案 0 :(得分:10)

Colorbox有一个“照片”选项。如果在构造函数中将此属性设置为true,则会强制它渲染照片。

$(target).colorbox({photo: true});

答案 1 :(得分:0)

您应该在客户端设置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; }
        }
    }

答案 2 :(得分:0)

看来我无法做我正在尝试使用带有ashx图像的colorbox。如果有人找到方法,请在此处发布。

我考虑过删除这个问题,但是如果其他人遇到同样的问题,我会把它留下来。

答案 3 :(得分:0)

在第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中对我来说很合适:)

相关问题