MVC3 - 从数据库中获取红色x而不是图片
在数据库中存储数据时,我得到红色的x标记而不是图片。我相信我在视图文件中有问题。请有人看看这个,并告诉我如何纠正它。如果我有错误的网址操作,请告诉我应该使用哪些网址。提前致谢。MVC3 - 从数据库中获取红色x而不是图片
SubCategory2表具有以下的列...
列字段>图片1:数据类型> VARBINARY(MAX)
列字段> ImageMimeType:数据类型> VARCHAR(50)
指数.cshtml文件
@foreach (var item in Model) {
<td>
<img src="@Url.Action("GetImage", "SubProductCategory2",
new { id = item.SubProductCategoryID})" alt="" height="100" width="100" />
</td>
Edit.cshtml文件 “编辑” 是在控制器上的方法。 “ProductCategoryL2”是控制器中的方法。 “GetImage”是控制器中的方法。所有这些方法都在叫ProductCategoryControllerL2
同一控制文件@using (Html.BeginForm("Edit", "ProductCategoryL2", "GetImage",
FormMethod.Post, new { @encType = "multipart/form-data" }))
{
<div class="editor-field">
<img src="@Url.Action("GetImage", "SubProductCategory2", new {
Model.SubProductCategoryID })" alt="" />
@Html.ValidationMessage("Picture1", "*")
<input type="file" id="fileUpload" name="Create" size="23"/>
</div>
}
ProductCategoryL2Controller.cs文件
[HttpPost]
public ActionResult Edit(int id, FormCollection collection,
SubProductCategory2 editSubProdCat, HttpPostedFileBase image)
{
var r = db.SubProductCategory2.First(x => x.SubProductCategoryID
== id);
if (TryUpdateModel(r))
{
if (image != null)
{
editSubProdCat.ImageMimeType = image.ContentType;
editSubProdCat.Picture1 = new byte[image.ContentLength];
image.InputStream.Read(editSubProdCat.Picture1, 0,
image.ContentLength);
}
db.SaveChanges();
return RedirectToAction("/");
}
return View(r);
}
public FileContentResult GetImage(int productId)
{
var product = db.SubProductCategory2.First(x =>
x.SubProductCategoryID == productId);
return File(product.Picture1, product.ImageMimeType);
}
增加注
我使用MVC 3框架。 GetImage方法已经从Steven Sanderson的书Pro ASP.NET MVC 2框架中提取出来。所以我不确定这是否会成为问题?
我想借此来调试的第一步将是直接尝试在浏览器的图像的URL。右键点击红色的X,复制网址并将其粘贴到地址栏中。如果URL看起来正确,那么应该是一个更好的错误,告诉你问题是什么。如果失败了,请在GetImage例程中放置一个断点,以确保路径正确,并且调用了方法。尝试Fiddler以查看正在发出的请求以及您的Web服务器在说什么。
我的猜测是你的行为错误。当您的方法位于ProductCategoryL2控制器上时,它看起来像链接到SubProductCategory2控制器上的GetImage操作。
另外我不明白你的Model.SubProductCategoryID值应该如何映射到你的productId参数。尝试改变这些调用:
Url.Action("GetImage", "SubProductCategory2",
new { id = item.ProductCategoryID})
Url.Action("GetImage", "SubProductCategory2", new {
Model.SubProductCategoryID })
这些:
Url.Action("GetImage", "ProductCategoryL2",
new { productId = item.ProductCategoryID})
Url.Action("GetImage", "ProductCategoryL2", new {
productId = Model.SubProductCategoryID })
@JasonGoematt - 我复制的URL,我得到以下消息...参数字典包含一个空的项为参数'productId'的非空类型'System.Int32'方法'System.Web.Mvc.FileContentResult GetImage (Int32)'在'MvcMyApplication.Controllers.ProductCategoryL2Controller'中。可选参数必须是引用类型,可为空类型,或者声明为可选参数。参数名称:参数 – DiscoDude 2011-03-18 20:12:04
听起来好像没有将“productId”参数作为路由值的一部分传递,请尝试在我的答案中使用代码。第一次在你的代码中设置'id',但你的参数名为'productId',在第二个里面似乎没有指定名称,我不知道这是如何工作的。 – 2011-03-18 22:19:07
@JasonGoematt - 我复制了你的代码,现在错误显示-Value不能为空或空。参数名称:contentType指向返回File(product.Picture1,product.ImageMimeType); – DiscoDude 2011-03-19 13:16:26
输入文件字段被称为Create
:
<input type="file" id="fileUpload" name="Create" size="23"/>
,而控制器动作参数处理表单提交被称为image
(带有HttpPostedFileBase
型)=>此参数将始终为null
在Edit
控制器行动,没有什么会被保存在数据库中。
该属性在Html.BeginForm
帮助程序中被称为enctype
而不是encType
。同样在GetImage
操作中,确保product.Picture1
表示正确的图像字节数组,并且其内容类型与product.ImageMimeType
匹配。
因此,例如为了进一步调试此问题,您可以尝试将其保存到某个临时文件,以在返回之前查看它是否为有效图像。另外,还要确保你已经从数据库中取出的product
实例不为空:
// if the content type is image/png:
File.WriteAllBytes(@"c:\foo.png", product.Picture1);
然后确保foo.png
开放成功地与一个图像浏览器。
如何确保product.Picture1代表正确的图像字节数组,并且它的内容类型与product.ImageMimeType相匹配? – DiscoDude 2011-03-18 18:43:30
您正试图将文件内容作为值返回给您的img的src属性。您的浏览器需要针对图像发出单独的请求。
视图改成这样:
<img src="GetImage/@(Model.SubProductCategoryID)" />
没有运气。更改了索引和编辑文件 – DiscoDude 2011-03-18 18:42:34
当您直接访问httx:// mysite/GetImage/i(其中,我是一个有图像的有效productId)时,您在浏览器中获得了什么? – 2011-03-22 14:19:46
你是不是使用MVC3?你为它加了标签V2。 .also标记您使用的数据库。 – gideon 2011-03-18 17:25:13
什么是product.Picture1的类型? – 2011-03-18 17:30:17
@DiscoDude:另外,对于失败的情况,product.ImageMimeType的值是多少? – 2011-03-18 17:36:41