【问题标题】:Error Occurs When Store File into Database将文件存储到数据库时发生错误
【发布时间】:2014-08-06 15:01:37
【问题描述】:

当我尝试上传文件时发生错误 输入不是有效的 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; }

【问题讨论】:

  • @WeakestInCoding...试试下面的答案,如果问题仍然存在,它会起作用,然后评论...thankzz
  • 你成功了,谢谢=D
  • 很高兴它成功了,谢谢...
  • 抱歉回复晚了,非常感谢!!!!!!!!!!!!!!!!!!!!!!!! =D

标签: c# asp.net asp.net-mvc asp.net-mvc-4 httppostedfilebase


【解决方案1】:

代替

&lt;%: Html.TextBoxFor(model =&gt; model.FileLocation, new { type="file"})%&gt;

以简单的html标签为:

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

这会很好..

【讨论】:

  • 如果name="FileLocation" 不是file 可能会起作用
  • @Serg Sagan,Erm 如何设置上传文件格式的验证
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
相关资源
最近更新 更多