【发布时间】:2010-07-06 21:36:09
【问题描述】:
我需要以这种方式在视图上显示图像
<img src = <% = Url.Action("GetImage", "Home", new { productID })%>
这是应该提供数据的操作
public FileContentResult GetImage(int ID)
{
var img = db.Images.Where(p => p.ID == ID).First();
return File(img.ImageData, img.ImageMimeType);
}
这个例子来自 Pro ASPNET.NET MVC (Steven Sanderson/APress)。我收到以下错误:System.Web.Mvc.Controller.File(string, string) 的最佳重载方法匹配有一些无效参数。 无法从 System.Data 转换。 Linq.Binary 转字符串。
然而,智能感知告诉我有一个重载方法(byte[] filecontents, string fileType)。但是,当我编写上面的代码时,我得到了错误。我错过了什么吗?
编辑
感谢您的回答。我在上传图像文件时遇到了类似的问题。这是我的操作方法
public ActionResult AddImage(HttpPostedFileBase image)
{
if(image != null)
{
var img = new Image();//This Image class has been
//created by the DataContext
img.ImageMimeType = image.ImageMimeType
img.ImageData = new byte[image.ContentLength];
image.InputStream.Read(img.ImageData, 0, image.ContentLength);
}
}
最后一行出现错误“image.InputStream.Read(myImage.ImageData, 0, image.ContentLength);” 说它不能转换 System.Data .Linq.Binary to Byte[]
我所做的是 (i) 创建一个名为 ImageDataClass 的新类,(ii) 对该类执行上述操作,(iii) 执行从 ImageDataClass 到 Image 的显式转换,以及(iv) 使用 Linq 保存到数据库。
我认为它不应该那么复杂。对于另一种情况,是否有任何方法可以仅使用 ToArray 之类的扩展方法使其工作?
感谢您的帮助
【问题讨论】:
标签: asp.net-mvc file-io