异步返回图像

时间:2011-01-04 12:34:35

标签: asp.net-mvc

我在Image对象中有图像,并希望使用jQuery异步请求在屏幕上显示图像。

请给我一些解决方案。

由于

Munish

1 个答案:

答案 0 :(得分:1)

您无需为此使用异步调用。一个简单的控制器动作返回图像:

public ActionResult Image()
{
    byte[] imageData  = ...
    return File(imageData, "image/jpeg");
}

然后将指向此控制器操作的<img>标记注入DOM:

$('#someButton').click(function() {
    // when some button is clicked show the image:
    var imageUrl = '<%= Url.Action("Image", "SomeController") %>';
    $('body').append(
        $('<img/>').attr('src', imageUrl).attr('alt', '')
    );
});

将执行异步请求并显示图像。