MVC如何将图片投放到响应流 [英] MVC how to serve images to response stream

查看:36
本文介绍了MVC如何将图片投放到响应流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我检索产品列表以及图像名称,然后将图像缩小到视图所需的大小.图像现在已在内存中,准备好写入响应流.我知道客户端将为每个图像发送一个响应,但是我不知道如何通过它来提供图像.

In my controller I retrieve a list of products along with an image name, then scale the image down to the size needed by the view. The images are now in memory, ready to be written to the response stream. I know the client will send a response for each image but I have no idea how to hook into it to provide the image.

查看代码:

    @foreach (var product in Model.Products)
    {
       @product.Name
       <img src="@product.Thumbnail"/>
       Priced From $@product.LowestPrice
    }

控制器:

    model.Products =
       DataContext.Products.Where(p => p.Category.Name
            .Equals(id)).Select(m => new ProductListItem
                {
                   Name = m.Name,
                   Thumbnail = ImageResizer.Resize(m.Image, 75, 100, <normally I put the output stream here>),
                   LowestPrice = SqlFunctions.StringConvert( m.PriceSet.Prices.Min(p =>p.Price1))
                }
    );

ImageResizer.Resize()签名在哪里

Where ImageResizer.Resize() signature is

Resize(string imageName, int width, int height, Stream outputStream)

所以我想我的问题应该是-我要输入什么图像名称,以及如何侦听可写入流中的每个图像的请求?

So my question I think should be- what do I put in for the image name and how do I listen for requests for each image that can be written to the stream?

推荐答案

获取有关新操作的路线/操作"链接,该链接下载图像以设置为图像url,

Get Route/Action link on new action which downloads an image to set as image url,

<img src='@Url.RouteUrl("Full", new { action = "Image", controller = "Media", number = product.id })' />

<img src='@Url.Action("Image", new { number = 3 })' />

添加具有类似

public ActionResult Image(int? number)
{
    var media = mr.GetMedia(number);

    return base.File(media.Content, media.ContentType ?? "image/jpeg");
}

其中media.Content是二进制内容或流引用

where media.Content is binary content or stream reference

这篇关于MVC如何将图片投放到响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆