0

尝试上传文件时出错输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。我的分配数据库属性>> FileLocation 是一个 varBinary(MAX) ,我做错了哪一部分?

控制器

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {
        if (Request.Files != null && Request.Files.Count == 1)
        {
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                var content = new byte[file.ContentLength];
                file.InputStream.Read(content, 0, file.ContentLength);
                assignment.FileLocation = content;
                // the rest of your db code here
            }

        }
        assignment.SubmissionDate = DateTime.Now;
        db.Assignments.Add(assignment);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

看法

    <% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
   { %>
    <%: Html.ValidationSummary(true) %>

...........

    <div class="editor-label">
        <%: Html.LabelFor(model => model.FileLocation) %>
    </div>
    <div class="editor-field">
       <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
    </div>

......

模型

public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    //[Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public byte[] FileLocation { get; set; }
4

1 回答 1

1

代替

<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>

以简单的 html 标签为:

<input type="file" name="file" id="file"/>

这会很好..

于 2014-08-06T16:23:25.927 回答