使用jquery渲染图像(流对象)
只是想知道,如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我今天正在研究这个问题并遇到了你的问题,这对我有帮助(也有伤害)。这是我发现的:
我最终使用了部分视图:
使用标准 mvc3 模板
Index.cshtml(摘录):
创建引用提供以下操作的部分视图图像:
HomeController 具有两种操作方法:
Jquery:
抱歉,我知道我试图使用名称空间的脚本文件中有一些额外的代码也。不管怎样,我想这个想法是你用ajax请求部分视图,其中包含图像而不是直接请求图像。
So I was working on this today and ran into your question, which helped (and hurt) me. Here is what I found:
I ended up using a partial view:
Using the standard mvc3 template
Index.cshtml (excerpt):
Create the partial view which makes reference to the action that provides the image:
HomeController with both action methods:
Jquery:
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.