【问题标题】:ASP.NET MVC File Upload Error - "The input is not a valid Base-64 string"ASP.NET MVC 文件上传错误 - “输入不是有效的 Base-64 字符串”
【发布时间】:2010-06-11 23:23:28
【问题描述】:

我正在尝试将文件上传控件添加到我的 ASP.NET MVC 2 表单,但在选择 jpg 并单击“保存”后,出现以下错误:

输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非空白字符。

这是视图:

<% using (Html.BeginForm("Save", "Developers", FormMethod.Post, new {enctype = "multipart/form-data"})) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            Login Name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.LoginName) %>
            <%: Html.ValidationMessageFor(model => model.LoginName) %>
        </div>

        <div class="editor-label">
            Password
        </div>
        <div class="editor-field">
            <%: Html.Password("Password") %>
            <%: Html.ValidationMessageFor(model => model.Password) %>
        </div>

        <div class="editor-label">
            First Name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.FirstName) %>
            <%: Html.ValidationMessageFor(model => model.FirstName) %>
        </div>

        <div class="editor-label">
            Last Name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.LastName) %>
            <%: Html.ValidationMessageFor(model => model.LastName) %>
        </div>

        <div class="editor-label">
            Photo
        </div>
        <div class="editor-field">
            <input id="Photo" name="Photo" type="file" />
        </div>

        <p>
            <%: Html.Hidden("DeveloperID") %>
            <%: Html.Hidden("CreateDate") %>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

还有控制器:

//POST: /Secure/Developers/Save/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(Developer developer)
        {
            //get profile photo.
            var upload = Request.Files["Photo"];
            if (upload.ContentLength > 0)
            {
                string savedFileName = Path.Combine(
                      ConfigurationManager.AppSettings["FileUploadDirectory"],
                      "Developer_" + developer.FirstName + "_" + developer.LastName + ".jpg");
                upload.SaveAs(savedFileName);
            }
            developer.UpdateDate = DateTime.Now;
            if (developer.DeveloperID == 0)
            {//inserting new developer.
                DataContext.DeveloperData.Insert(developer);
            }
            else
            {//attaching existing developer.
                DataContext.DeveloperData.Attach(developer);
            }
            //save changes.
            DataContext.SaveChanges();
            //redirect to developer list.
            return RedirectToAction("Index");
        }

谢谢, 贾斯汀

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我最近找到了一个解决方案,虽然我现在使用的是 MVC3 而不是 MVC2。

    在保存操作中,从绑定对象中排除二进制字段,并包含一个单独的 HttpPostedFileBase 字段:

    例如

    public ActionResult Save([Bind(Exclude = "Photo")]Developer developer, HttpPostedFileBase Photo) {...}
    

    请注意,您也可以这样做以避免在视图中包含 Html.Hidden 元素。例如:

    public ActionResult Save([Bind(Exclude = "Photo,DeveloperID,CreateDate")]Developer developer, HttpPostedFileBase Photo) {...}
    

    然后您可以直接使用此 HttpPostedFileBase 对象,而无需访问 Request.Files。

    就个人而言,我实际上使用以下代码将这些类型的图像存储在数据库中的 SQL“图像”字段中:

    if (Picture != null)
    {
        if (Picture.ContentLength > 0)
        {
            byte[] imgBinaryData = new byte[Picture.ContentLength];
            int readresult = Picture.InputStream.Read(imgBinaryData, 0, Picture.ContentLength);
            Developer.Picture = imgBinaryData;
        }
    }
    

    希望这有帮助...

    标记

    【讨论】:

      【解决方案2】:

      我刚刚尝试了您的代码,并且能够毫无问题地上传。我没有保存到数据库中,我的 Developer 类也没有 Photo 属性。

      namespace MvcApplication5.Controllers
      {
          public class Developer
          {
              public string FirstName { get; set; }
              public string LastName { get; set; }
              public DateTime UpdateDate { get; set; }
              public int DeveloperID { get; set; }
              public string LoginName { get; set; }
              public string Password { get; set; }
          }
      }
      

      控制器

      public class DefaultController : Controller
      {
          //
          // GET: /Default/
      
          [AcceptVerbs(HttpVerbs.Get)]
          public ActionResult Index()
          {
              return View();
          }
      
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult Save(Developer developer)
          {
              //get profile photo. 
              var upload = Request.Files["Photo"];
              if (upload.ContentLength > 0)
              {
                  string savedFileName = Path.Combine(
                        @"C:\temp",
                        "Developer_" + developer.FirstName + "_" + developer.LastName + ".jpg");
                  upload.SaveAs(savedFileName);
              }
              developer.UpdateDate = DateTime.Now;
              if (developer.DeveloperID == 0)
              {//inserting new developer. 
      
              }
              else
              {//attaching existing developer. 
      
              }
              //save changes. 
      
              //redirect to developer list. 
              return RedirectToAction("Index");
          }
      
      }
      

      查看

      <div>
              <% using (Html.BeginForm("Save", "Default", FormMethod.Post, new { enctype = "multipart/form-data" }))
                 { %>
              <%: Html.ValidationSummary(true)%>
              <fieldset>
                  <legend>Fields</legend>
                  <div class="editor-label">
                      Login Name
                  </div>
                  <div class="editor-field">
                      <%: Html.TextBoxFor(model => model.LoginName)%>
                      <%: Html.ValidationMessageFor(model => model.LoginName)%>
                  </div>
                  <div class="editor-label">
                      Password
                  </div>
                  <div class="editor-field">
                      <%: Html.Password("Password")%>
                      <%: Html.ValidationMessageFor(model => model.Password)%>
                  </div>
                  <div class="editor-label">
                      First Name
                  </div>
                  <div class="editor-field">
                      <%: Html.TextBoxFor(model => model.FirstName)%>
                      <%: Html.ValidationMessageFor(model => model.FirstName)%>
                  </div>
                  <div class="editor-label">
                      Last Name
                  </div>
                  <div class="editor-field">
                      <%: Html.TextBoxFor(model => model.LastName)%>
                      <%: Html.ValidationMessageFor(model => model.LastName)%>
                  </div>
                  <div class="editor-label">
                      Photo
                  </div>
                  <div class="editor-field">
                      <input id="Photo" name="Photo" type="file" />
                  </div>
                  <p>
                      <%: Html.Hidden("DeveloperID")%>
                      <%: Html.Hidden("CreateDate")%>
                      <input type="submit" value="Save" />
                  </p>
              </fieldset>
              <%} %>
          </div>
      

      【讨论】:

      • 这应该不是问题,因为错误是在进入控制器操作之前引发的,因此与保存到数据库无关。我不知所措 b/c 我认为这个错误与没有 enctype = "multipart/form-data" 相关,我显然有......
      • 实际上你给了我修复它所需的东西。您没有 Photo 属性,而我有 byte[] Photo 属性,因此它试图将文件上传分配给它,这显然不起作用。我重命名了文件上传控件 ProfilePhoto,然后它就起作用了。
      【解决方案3】:

      我遇到了同样的问题。这是我找到的解决方案。 类属性:

      public byte[] Logo { get; set; }
      

      查看代码:

      @using (Html.BeginForm("StoreMyCompany", "MyCompany", FormMethod.Post, new { id = "formMyCompany", enctype = "multipart/form-data" }))
      {
       <div class="form-group">
        @Html.LabelFor(model => model.modelMyCompany.Logo, htmlAttributes: new { @class = "control-label col-md-3" })
          <div class="col-md-6">
          <input type="file" name="Logo" id="fileUpload" accept=".png,.jpg,.jpeg,.gif,.tif" />
          </div>
       </div>
       }
      

      控制器代码:

       public ActionResult StoreMyCompany([Bind(Exclude = "Logo")]MyCompanyVM model)
          {
              try
              {
                  Company objCompany = new Company();
      
                  byte[] imageData = null;
                  if (Request.Files.Count > 0)
                  {
                      HttpPostedFileBase objFiles = Request.Files["Logo"];
      
                      using (var binaryReader = new BinaryReader(objFiles.InputStream))
                      {
                          imageData = binaryReader.ReadBytes(objFiles.ContentLength);
                      }
                  }
              }
              catch (Exception ex)
              {
                  Utility.LogError(ex);
              }
      
              return View();
          }
      }
      

      我刚刚从控制器的调用中排除了 Logo。

      【讨论】:

        【解决方案4】:

        我有同样的错误,但上面的解决方案对我不起作用,相反我注意到我的模型属性名称与我通过控制器传递的参数名称相同:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditSOP(File file, HttpPostedFileBase Upload)
        

        我的模型属性名称是

        public byte[] Upload { get; set; }
        

        用不同的参数名称重命名后,它现在可以工作了:

        public ActionResult EditSOP(File file, HttpPostedFileBase UploadFile)
        

        【讨论】:

          猜你喜欢
          • 2016-02-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多