jquery - ashx 控件返回图像,jquery 在加载时获取大小或 .NET OnLoad 方法不起作用

标签 jquery onload ashx pageload

我有一个 ASHX 处理程序在页面加载时返回图像。我需要根据图像的尺寸动态向图像添加一个类。我尝试使用以下方法执行此操作:

代码隐藏方法

CS

protected void Page_Load(object sender, EventArgs e)
{
    int id = Convert.ToInt32(Request.QueryString["id"]);
    image1.ImageUrl = "get_image.ashx?id=" + id;
}

public void classify_image_Load(object sender, EventArgs e)
{
    if(image1.Width.Value > image1.Height.Value)
    {
        image1.CssClass = "landscape";
    }
    else
    {
        image1.CssClass = "portrait";
    }
}

html

<asp:Image ID="image1" runat="server" OnLoad="classify_image_Load" />

这适用于初始加载,但是在任何回发(上传新图像/旋转/裁剪)时,它无法正确应用该类。

jQuery方法

js

$(window).load(function(){

    $('#image1').load(function() {
        if($(this).width() > $(this).height())
        {
            $(this).attr('class', 'landscape');
        } else {
            $(this).attr('class', 'portrait');
        }
    });

});

这个方法根本不起作用,图像没有分配给它的类。我不确定这是否是 ashx 控件的计时问题还是什么。

ASHX 代码

public class get_image : IHttpHandler
{
    string file_path = ConfigurationManager.AppSettings["file_path"].ToString();
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        Image img;
        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            int id;
            if (context.Request.QueryString["id"].IndexOf('?') > 0)
            {
                id = Int32.Parse(context.Request.QueryString["id"].Split('?')[0]);
            }
            else
            {
                id = Int32.Parse(context.Request.QueryString["id"]);
            }

            dbclassDataContext db = new dbclassDataContext();
            photo d = (from p in db.photos
                       where p.id == id
                       select p).SingleOrDefault();

            if (d != null)
            {
                if (!String.IsNullOrEmpty(d.filename))
                {
                    img = Image.FromFile(file_path + "\\" + d.filename);
                    context.Response.ContentType = "image/" + d.filetype;
                    img.Save(context.Response.OutputStream, get_format(d.filetype));
                }
                else
                {
                    img = Image.FromFile(file_path + "\\no_image.jpg");
                    context.Response.ContentType = "image/jpeg";
                    img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }
            }
            else
            {
                img = Image.FromFile(file_path + "\\no_image.jpg");
                context.Response.ContentType = "image/jpeg";
                img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }
        else
        {
            img = Image.FromFile(file_path + "\\no_image.jpg");
            context.Response.ContentType = "image/jpeg";
            img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
        img.Dispose();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private ImageFormat get_format(string ftype)
    {
        switch (ftype)
        {
            case "jpeg":
                return ImageFormat.Jpeg;
            case "png":
                return ImageFormat.Png;
            case "gif":
                return ImageFormat.Gif;
            default:
                return ImageFormat.Jpeg;
        }
    }
}

我使用 Linq 根据用户 ID 从数据库中提取位置和类型,然后将图像返回到请求页面。这似乎工作正常,但我将其包含在内,以防可能存在我忽略的任何问题。

问题

我需要根据图像的尺寸对图像进行动态分类,我可以在 .NET 代码后面或使用 jQuery 来完成此操作。我上述方法做错了什么导致它无法正常工作?

最佳答案

如果从浏览器缓存加载图像,.load 事件可能会出现问题(请参阅 http://api.jquery.com/load-event/ ) - 也许您可以尝试禁用缓存或向图像附加时间值,以便浏览器不会缓存它,万一就是这个问题呢?

关于jquery - ashx 控件返回图像,jquery 在加载时获取大小或 .NET OnLoad 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4651462/

相关文章:

javascript - 为什么 onload 事件不起作用?

页面渲染后执行 Javascript IE6

下载文件的 ASP.NET 处理程序 (ashx) 与 MVC Controller 操作

javascript - 如何定期检查直到 ajax 返回状态值发生变化?

javascript - 使用 jquery 及其选项的 dropzone

javascript - fullpage js 和 AOS - 不能一起工作

asp.net - 从通用处理程序 (.ashx) 进行异步调用

javascript - 获取最近选择框的值

javascript - 为什么 window.onload 在 IE 中不触发

c# - ashx 文件的输出(服务器端)缓存