2011-04-21 105 views
5

我正嘗試使用Create方法將圖像保存到數據庫。但是當試試這個代碼時,我得到這個錯誤:使用ASP.NET MVC將圖像保存到數據庫中

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.*

我很初學MVC。我會非常感謝迴應,非常感謝提前。

[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(); 
    } 
} 

這裏是我創建的視圖代碼:

<%@ 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文件:

<appSettings> 
    <add key="FileUploadDirectory" value="~/Resources/images/customers/" /> 
</appSettings> 

數據庫條目:

Column Name Data Type Allow Nulls 
ImageData image  yes 
+0

你必須在將它保存到任何地方之前進行轉換。 – wegginho 2011-04-21 12:02:49

+0

感謝您的回覆,請您解釋一下,我可以在哪裏添加此代碼。你可以請添加下面的代碼到我的例子中。這樣我才能清晰地理解。 – JAML 2011-04-21 12:16:25

+0

您可以發佈將圖像寫入請求的數據嗎? – wegginho 2011-04-21 12:29:58

回答

1

試試這個:的

string savedFileName = Server.MapPath("/Resources/images/customers/" + "customers_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg"); 

代替

string savedFileName = Path.Combine(
         ConfigurationManager.AppSettings["FileUploadDirectory"], 
         "customers_" + saveCustomer.FirstName + "_" + saveCustomer.LastName + ".jpg"); 
  1. 如果您的用戶模型包含圖像字段,它沒有必要保存到服務器端DIRS。

  2. 形式的崗位不應該有上傳文件字段,請控制器更改爲:

===================== ===========

[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(); 
    } 
} 
+0

更改爲您的代碼,但仍然得到相同的錯誤:-( – JAML 2011-04-21 12:22:28

+0

由於它幾乎相同,你只是改變了路徑...(?) – wegginho 2011-04-21 12:28:58

+0

是的,這就是我所做的,改變了一行代碼,但沒有你好,請你檢查我所有的代碼文件,我正在做正確的方式嗎? – JAML 2011-04-21 12:32:44

0
private byte[] ImageToBytes(Image img, ImageFormat format) 
{    
MemoryStream mstream = new MemoryStream(); 
img.Save(mstream, format); 
mstream.Flush(); 
return mstream.ToArray(); 
} 
相關問題