如前所述,我正在尝试在表中插入图像,其中字段的类型是 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);
}
但是当我尝试上传图像(通过单击创建按钮)时,我有以下内容:
条目不是有效的 base64 字符串,因为它包含不是 base 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();
}
}
但他们都没有工作。我究竟做错了什么 ?