使用 ASP.NET MVC 在详细信息页面模板中显示数据库中的 byte[] 数组图像

发布于 2024-11-02 11:56:06 字数 748 浏览 9 评论 0原文

当我尝试使用以下命令在“详细信息”页面模板内显示 byte[] 数组图像时:

public FileContentResult RenderPhoto(byte[] photo)
{
    // var array = (byte[])Session["photo"];
    // return File(array, "image/jpeg");

    return File(photo, "image/jpeg");
}

<img src="@Url.Action("RenderPhoto", Model.Photo)"/>

photo 为 null。

如果我将 Student.Photo 存储在 Session: 中

//
// GET: /Student/Details/5

public ViewResult Details(int id)
{
    Student student = db.Students.Find(id);

    Session["photo"] = student.Photo;

    return View(student);
}

,并尝试显示从 Session 中检索值的图像(上面的注释行),它就可以工作。

为什么我在第一种情况下得到空值?

将学生传递给 ViewResult Details(int id) 中的视图后,Model.Photo 不再保留该值?

When I try to display a byte[] array image inside a Details page template using:

public FileContentResult RenderPhoto(byte[] photo)
{
    // var array = (byte[])Session["photo"];
    // return File(array, "image/jpeg");

    return File(photo, "image/jpeg");
}

<img src="@Url.Action("RenderPhoto", Model.Photo)"/>

photo is null.

If I store student.Photo in Session:

//
// GET: /Student/Details/5

public ViewResult Details(int id)
{
    Student student = db.Students.Find(id);

    Session["photo"] = student.Photo;

    return View(student);
}

and try to display the image retrieving the value from Session (commented lines above) it works.

Why I'm getting a null value in the first case?

After passing student to the View in ViewResult Details(int id), Model.Photo doesn't hold that value anymore?

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

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

发布评论

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

评论(2

等风来 2024-11-09 11:56:07

首先

Url.Action("RenderPhoto", Model.Photo)

不会工作,Model.Photo(大概是你的字节数组)将被视为一个对象来推断其路线值。它将生成一个具有 Array 对象的公共属性的路由,可能类似于

?IsFixedSize=true&IsReadOnly=false&Length=255

That's will be a very unhelpful url。一旦页面在浏览器中加载,浏览器将请求该图像,调用您的 RenderPhoto 方法,但是没有名为 photo 的参数,因此绑定将失败,即使有一个名为 photo 的参数(AFAIK),也没有逻辑DefaultModelBinder 用于从字符串创建字节数组,因此 photo 为 null。

您需要做的是将带有 Id 属性的匿名对象传递到 Url.Action 中,

Url.Action("RenderPhoto", new { Id = Model.PhotoId })

该对象将被转换为查询字符串,可能类似于以下内容(但这取决于您的路线)

/xxx/RenderPhoto/25

然后您需要检索该数据RenderPhoto 方法中的照片

Martin

First of all

Url.Action("RenderPhoto", Model.Photo)

Will not work, Model.Photo (presumably your byte array) will be treated as an Object to infer route values of. It'll generate a route with the public properties of an Array object, probably along the lines of

?IsFixedSize=true&IsReadOnly=false&Length=255

That is going to be a pretty unhelpful url. Once the page loads in the browser, the browser will request that image, calling your RenderPhoto method, but there is no parameter called photo, so binding would fail, and even if there was a parameter called photo there (AFAIK) no logic in the DefaultModelBinder for creating a byte array from a string, hence photo is null.

What you need to do is pass an anonymous object with an Id property into Url.Action

Url.Action("RenderPhoto", new { Id = Model.PhotoId })

That will be converted to a querystring possibly along the lines of the following (but it depends on your routes)

/xxx/RenderPhoto/25

And you then need to retreive the data for the photo in your RenderPhoto method

Martin

无人接听 2024-11-09 11:56:06

为什么我在第一种情况下得到空值?

您不能在 标记中将字节数组传递到服务器。 标签只是向指定资源发送 GET 请求。正确的方法是使用 id:

<img src="@Url.Action("RenderPhoto", new { photoId = Model.PhotoId })" />

然后:

public ActionResult RenderPhoto(int photoId)
{
    byte[] photo = FetchPhotoFromDb(photoId);
    return File(photo, "image/jpeg");
}

Why I'm getting a null value in the first case?

You cannot pass a byte array to the server in an <img> tag. The <img> tag simply sends a GET request to the designated resource. The correct way to do this is to use an id:

<img src="@Url.Action("RenderPhoto", new { photoId = Model.PhotoId })" />

and then:

public ActionResult RenderPhoto(int photoId)
{
    byte[] photo = FetchPhotoFromDb(photoId);
    return File(photo, "image/jpeg");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文