ASP.NET MVC文件上传错误 - "输入不是一个有效的Base-64串" [英] ASP.NET MVC File Upload Error - "The input is not a valid Base-64 string"

查看:128
本文介绍了ASP.NET MVC文件上传错误 - "输入不是一个有效的Base-64串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个文件上传控件添加到我的ASP.NET MVC 2的形式,但之后,我选择JPG和单击保存,它提供了以下错误:

I'm trying to add a file upload control to my ASP.NET MVC 2 form but after I select a jpg and click Save, it gives the following error:

输入不是一个有效的Base-64字符串,因为它包含非基本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");
        }

谢谢,
贾斯汀

Thanks, Justin

推荐答案

我只是想你code,并能没有任何问题上传。我没有保存到数据库中,也没有我的开发类有照片属性。

I just tried your code and was able to upload without any issues. I did not save to the database nor does my Developer class have a Photo property.

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>

这篇关于ASP.NET MVC文件上传错误 - &QUOT;输入不是一个有效的Base-64串&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆