[英]Save image to database with ASP.NET MVC
I am trying to save image to database with Create method. 我正在尝试使用Create方法将图像保存到数据库。 but when try this code, I get this error: 但尝试此代码时,我收到此错误:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.* 输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非空白字符。*
I am very beginner to MVC. 我是MVC的初学者。 I will really appreciate for the response, Many thanks in advance. 我将非常感谢您的回复,非常感谢您提前。
[Authorize]
[HttpPost]
public ActionResult Create(Customers saveCustomer)
{
try
{
// TODO: Add insert logic here
var upload = Request.Files["ImageData"];
if (upload.ContentLength > 0)
{
string savedFileName = Path.Combine(
ConfigurationManager.AppSettings["FileUploadDirectory"],
"customers_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg");
upload.SaveAs(savedFileName);
}
_db.Customers.InsertOnSubmit(saveCustomer);
_db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Here is my create view code: 这是我的创建视图代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Reservation.Models.Customers>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Create</h2>
<% using (Html.BeginForm("Create", "Customers", FormMethod.Post, new {enctype="multipart/form-data"})) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Add new customer record</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.FirstName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.LastName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LastName) %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Email) %>
<%: Html.ValidationMessageFor(model => model.Email) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Phone) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Phone) %>
<%: Html.ValidationMessageFor(model => model.Phone) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.CustomerNote) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CustomerNote) %>
<%: Html.ValidationMessageFor(model => model.CustomerNote) %>
</div>
<div>
<input type="file" id="ImageData" name="ImageData" />
</div>
<p>
<input type="submit" value="Add recrod" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
Web.config: Web.config文件:
<appSettings>
<add key="FileUploadDirectory" value="~/Resources/images/customers/" />
</appSettings>
Database entry: 数据库录入:
Column Name Data Type Allow Nulls
ImageData image yes
Try this: 尝试这个:
string savedFileName = Server.MapPath("/Resources/images/customers/" + "customers_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg");
instead of 代替
string savedFileName = Path.Combine(
ConfigurationManager.AppSettings["FileUploadDirectory"],
"customers_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg");
If your Customer Models contains the Image field, it's not necessary to save to server-side Dirs. 如果您的客户模型包含图像字段,则无需保存到服务器端目录。
the form post should not have the upload file field, please change the Controller to: 表单帖子不应该有上传文件字段,请将Controller更改为:
================================ ================================
[Authorize]
[HttpPost]
public ActionResult Create([Bind(Exclude = "ImageData")]Customers saveCustomer, HttpPostedFileBase ImageData)
{
try
{
// TODO: Add insert logic here
var upload = Request.Files["ImageData"];
string savedFileName = ""; //string for saving the image server-side path
if (upload.ContentLength > 0)
{
savedFileName = Server.MapPath("/Resources/images/customers/" + "customer_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg"); //get the server-side path for store image
upload.SaveAs(savedFileName); //*save the image to server-side
}
var index = savedFileName.IndexOf(@"\Resources\");
saveCustomer.ImageData = savedFileName.Substring(index, savedFileName.Length - index); //set the string of image server-side path to add-object
_db.Customers.InsertOnSubmit(saveCustomer); // save all field to databae (includes image server-side path)
_db.SubmitChanges(); // save database changes
return RedirectToAction("Index");
}
catch
{
return View();
}
}
private byte[] ImageToBytes(Image img, ImageFormat format)
{
MemoryStream mstream = new MemoryStream();
img.Save(mstream, format);
mstream.Flush();
return mstream.ToArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.