如何使用的HtmlHelper讲座ASP.NET MVC来显示图像 [英] How to display Images using HtmlHelper Class- ASP.NET MVC

查看:160
本文介绍了如何使用的HtmlHelper讲座ASP.NET MVC来显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前坚持使用HtmlHelper类显示图像的这个问题。

I am currently stuck with this problem of displaying an image using HtmlHelper class.

下面是我。

我有一个应该显示的图像的自定义HtmlHelper类:

I have a custom HtmlHelper class which supposed to display an image:

public static string Images(this HtmlHelper helper, ......){

    var writer = new HtmlTextWriter(new StringWriter());
    byte[] bytearray = ... // some image byte array retrieved from an object.

    // begin html image tag - this is where the problem is
    writer.AddAttribute(HtmlTextWriterAttribute.Src, url.Action("GetPhoto", "Clinical", new { image = bytearray })); 
    writer.RenderBeginTag(HtmlTextWriterTag.Img); 
    writer.RenderEndTag();
    // end of image tag

    return writer.InnerWriter.ToString();
}

所以我尝试了上述做的是注入Url.Action到IMG源属性。

So what i tried to do above is to inject a Url.Action into the img source attribute.

我有一个控制器GetPhoto即suppsoed来处理字节组,并返回一个图像。

I have a controller "GetPhoto" that is suppsoed to handle that bytearray and return an image.

public FileContentResult GetPhoto(byte[] image)
        {

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

我设法给控制器,但图像显示为空。有没有办法解决它?或者一个更好的方式来做到这一点?您的帮助将是pciated非常AP $ P $,谢谢!

I managed to get to the controller, but image shows as null. Is there a way around it ? or maybe an even better way to do this? Your help will be very much appreciated, Thanks!

推荐答案

有一些问题,你的控制器动作。该公司预计将图像传递一个字节数组。在助手类你想生成指向该控制器操作的 IMG 标签,并通过一个字节数组,但是这不会工作,因为你不能使用GET请求传递字节数组。您需要更改这个控制​​器动作,这样,而不是接受一个字节数组需要例如一些标识,可以让你获取的图像,并将其写入到输出流。然后生成 IMG 标签,当你的HTML帮助将通过该标识符。

There are some problems with your controller action. It expects to pass the image as a byte array. In your helper class you are trying to generate an img tag pointing to this controller action and passing a byte array but this won't work because you cannot pass byte arrays using GET requests. You need to change this controller action so that instead of accepting a byte array it takes for example some identifier that allows you to fetch the image and write it to the output stream. Then your html helper will pass this identifier when generating the img tag.

public FileContentResult GetPhoto(string imageId)
{
    byte[] image = _repository.GetPhoto(imageId);
    return File(image, "image/jpeg");
}

和你的助手:

public static string Images(this HtmlHelper htmlHelper, string id,string alt)
{
    var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; 
    var photoUrl = urlHelper.Action("GetPhoto", "Clinical", new { imageId = id });
    var img = new TagBuilder("img");
    img.MergeAttribute("src", photoUrl);
    img.MergeAttribute("alt", alt);
    return img.ToString(TagRenderMode.SelfClosing);
}

这可能会在您的视图中使用:

Which could be used in your view:

<%= Html.Images("123") %>

这篇关于如何使用的HtmlHelper讲座ASP.NET MVC来显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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