使用jquery渲染图像(流对象)

发布于 2024-11-07 23:46:11 字数 1404 浏览 0 评论 0原文

只是想知道,如何使用 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");

        }

问题

操作方法正在返回流,但未在页面上呈现。 我在这里错过了一些东西...

感谢您的关注。

Just wondering, how can I bind an image (stream) returned by an action method in an ajax call to an image element using jquery.

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');
            }
        });

Action method:

  public FileContentResult GetPersonImage(Person person)
        {

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

        }

Problem:

The Action method is returning the stream, but it is not getting rendered on the page.
I am I missing some thing here...

Thanks for looking in to it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2024-11-14 23:46:11

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

  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请求部分视图,其中包含图像而不是直接请求图像。

So I was working on this today and ran into your question, which helped (and hurt) me. Here is what I found:

  1. Like Eben mentioned you cant have the json data type. but thats only part of the problem. When you try to $('#idImgPerson').attr("src", data); as in your code you will just set the src attribute to the bytes returned by the call do the GetPersonImage() method.

I ended up using a partial view:

Using the standard mvc3 template

Index.cshtml (excerpt):

<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" />

Create the partial view which makes reference to the action that provides the image:

@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 with both action methods:

 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);
        }
    });

 };
 });

sorry I know there is a little bit of extra code in the script file i was trying to play with namespaces too. Anyhow, i guess the idea is you request the partial view with ajax, which contains the image rather than requesting the image directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文