更新时间:2023-01-06 19:43:43
有一些问题,你的控制器动作。该公司预计将图像传递一个字节数组。在助手类你想生成指向该控制器操作的 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") %>