【发布时间】: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