【发布时间】:2013-07-31 09:25:13
【问题描述】:
如前所述,我正在尝试在表格中插入图像,其中字段的类型是 Varbinary。
到目前为止我做了什么:
我有一个包含许多字段的表单:
@using (Html.BeginForm("InsertProduct", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>PRODUCT</legend>
<div class="editor-label">
@Html.LabelFor(model => model.PRODUCT_ID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PRODUCT_ID)
@Html.ValidationMessageFor(model => model.PRODUCT_ID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PRODUCT_NAME)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PRODUCT_NAME)
@Html.ValidationMessageFor(model => model.PRODUCT_NAME)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PRODUCT_IMAGE)
</div>
<div class="editor-field">
<input type="file" name="PRODUCT_IMAGE" id="PRODUCT_IMAGE" style="width: 100%;" />
</div>
<p>
<input type="submit" value="Create" class="btn btn-primary"/>
</p>
</fieldset>
}
所有这些字段都允许我在我的控制器中构造一个 PRODUCT 对象:
public ActionResult InsertProduct(PRODUCT ord)
{
MigrationEntities1 sent = new MigrationEntities1();
sent.PRODUCT.Add(ord);
sent.SaveChanges();
List<PRODUCT> Products = sent.PRODUCT.ToList();
return View("Products", Products);
}
但是当我尝试上传图片时(通过点击创建按钮),我有以下内容:
entry 不是有效的 base64 字符串,因为它包含一个字符 这不是基数 64
首先:这是处理图像的正确方法,其次,我认为我需要对我的图像进行预处理以插入它:如何操作?
谢谢!
编辑:
感谢收到的答案,似乎很适合插入。但是对于显示,我仍然有问题(仅显示“未找到图像”图)。我尝试了两种方法:
1.
<img src="LoadImage?id=@Model.product.PRODUCT_ID"/>
并在控制器中
public Image LoadImage(int id)
{
String serviceAddress = ConfigurationManager.AppSettings["WCFADDRESS"];
DataServiceContext context = new DataServiceContext(new Uri(serviceAddress));
PRODUCT product = context.Execute<PRODUCT>(new Uri(serviceAddress + "prod_id?prod_id=" + id)).ToList().FirstOrDefault();
MemoryStream ms = new MemoryStream(product.PRODUCT_IMAGE);
Image img = Image.FromStream(ms);
return img;
}
还有 2。:
@{
if (Model.product.PRODUCT_IMAGE != null)
{
WebImage wi = new WebImage(Model.product.PRODUCT_IMAGE);
wi.Resize(700, 700,true, true);
wi.Write();
}
}
但它们都不起作用。我究竟做错了什么 ?
【问题讨论】:
-
阅读 Remus Rusanu 的 this blog post。
标签: sql-server image linq asp.net-mvc-4 razor