【问题标题】:Rendering image(stream object) using jquery使用 jquery 渲染图像(流对象)
【发布时间】:2011-05-17 15:43:26
【问题描述】:

只是想知道,如何使用 jquery 将 ajax 调用中的操作方法返回的图像(流)绑定到图像元素。

JavaScript

$(document).ready($(function () {
        $(':input').change(function () {

                // object
                var Person = {
                            Name: 'Scott',
                            Address: 'Redmond'
                        };
                // Function for invoking the action method and binding the stream back
                ShowImage(Person);
    });
}));

 function ShowImage(Person) {


        $.ajax({
            url: '/Person/GetPersonImage',
            type: "Post",
            data: JSON.stringify(Person),
            datatype: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {

                // idImgPerson is the id of the image tag
                $('#idImgPerson')
                   .attr("src", data);

            },
            error: function () {
                alert('Error');
            }
        });

动作方法

  public FileContentResult GetPersonImage(Person person)
        {

            // Get the image    
            byte[] myByte = SomeCompnent.GetPersonImage(person);
            return File(myByte, "image/png");

        }

问题

Action 方法正在返回流,但它没有在页面上呈现。 我在这里错过了一些东西......

感谢您的关注。

【问题讨论】:

  • 您正在使用datatype: "json"。根据指定您希望服务器返回的数据类型的 API 文档。尝试删除它。

标签: asp.net-mvc image jquery rendering


【解决方案1】:

所以我今天正在处理这个问题,遇到了你的问题,这对我有帮助(也伤害了我)。这是我发现的:

  1. 就像 Eben 提到的,您不能拥有 json 数据类型。但这只是问题的一部分。当您尝试 $('#idImgPerson').attr("src", data);在您的代码中,您只需将 src 属性设置为调用 GetPersonImage() 方法返回的字节即可。

我最终使用了局部视图:

使用标准的 mvc3 模板

Index.cshtml(摘录):

<h3>Using jquery ajax with partial views</h3>
<div id="JqueryAjax">
<input class="myButton" data-url='@Url.Action("GetImagesPartial","Home")' name="Button1" type="button" value="button" />

创建引用提供图像的操作的局部视图:

@model string

<div>
<img  id="penguinsImgActionJquery"  alt="Image Missing" width="100" height="100" 
src="@Url.Action("GetPersonImage", "Home", new { someString=Model })" />
<br />
<label>@Model</label>
</div>

具有两种操作方法的 HomeController:

 public ActionResult GetImagesPartial(string someImageName)
    {

        return PartialView((object)someImageName);
    }

    public FileContentResult GetPersonImage(string someString)
    {

        var file = System.Web.HttpContext.Current.Server.MapPath(localImagesPath + "Penguins.jpg");
        var finfo = new FileInfo(file);
        byte[] myByte = Utilities.FileManagement.FileManager.GetByteArrayFromFile(finfo);
        return File(myByte, "image/png");

    } 

Jquery:

$(document).ready(function () {
$('.myButton').click(function (e) {
    var bleh = $(this).attr("data-url");
    e.preventDefault();
    someData.runAjaxGet(bleh);
});

var someData = {
    someValue: "value",
    counter: 1,
};
someData.runAjaxGet = function (url) {
    _someData = this;
    $.ajax({
        url: url,
        type: "Get",
        data: { someImageName: "ImageFromJQueryAjaxName" + _someData.counter },
        success: function (data) {
            $('#JqueryAjax').append(data);
            _someData.counter = _someData.counter + 1;
        },
        error: function (req, status) {
            alert(req.readyState + " " + req.status + " " + req.responseText);
        }
    });

 };
 });

抱歉,我知道脚本文件中有一些额外的代码,我也尝试使用命名空间。无论如何,我想这个想法是您使用 ajax 请求部分视图,其中包含图像而不是直接请求图像。

【讨论】:

    猜你喜欢
    • 2018-05-12
    • 2018-11-21
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多