【问题标题】:C# MVC 4 ASP.net, How to Insert File into Database store in (binary)C# MVC 4 ASP.net,如何将文件插入数据库存储(二进制)
【发布时间】:2014-08-06 12:54:03
【问题描述】:

有人介意指导我,如何将文件保存到我的数据库中,并尽可能检索它, 我还是这个 C# 和 MVC 4 的新手。 我的数据库分配包含一个名为 FileLocation 的属性,它是 varBinary (MAX)。

型号

 public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }



    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; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}
}

控制

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {

        if (ModelState.IsValid)
        {
            db.Assignments.Add(assignment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(assignment);
    }

查看

@using(Html.BeginForm("Create","Assignment",FormMethod.Post,new {enctype="multipart/form-data"}))
{
...
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
</div>
...
}

【问题讨论】:

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


    【解决方案1】:

    您需要在这里进行更多处理。上传的文件以 HttpPostedFileBase 的形式出现,而不是 byte[],因此您需要从 HttpPostedFileBase 的 InputStream 中获取 byte[],如下所示:

    [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
            }
        }
        return RedirectToAction("Create");    
    }
    

    附:该模型看起来可疑地像一个实体对象。使用 Entity 对象作为模型是一个非常糟糕的主意。尝试制作一个中间模型并使用它来呈现您的数据。

    编辑

    在你看来,改变这个:

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

    到这里:

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

    您遇到的错误是因为模型绑定器错误地尝试将页面上的 FileLocation 字段(HttpPostedFileBase 类型)绑定到模型中的 FileLocation 字段(byte[] 类型)。

    【讨论】:

    • 输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。当我尝试此代码时发生此错误
    • 你知道是什么导致了这个错误吗?我的数据库属性是 varbinary(MAX) 而模型是 byte[]
    • @WeakestInCoding 你知道错误发生在代码的哪一行吗?你能告诉我你现在写的整个方法吗?
    • 我写的和上面一模一样,当我保存文件时,出现错误,
    • @WeakestInCoding 查看我上面的编辑以获得最可能的解决方案。
    【解决方案2】:

    在 Asp.Net MVC 中,我们必须使用 HttpPostedFileBase 来上传文件,如下所示:-

    [HttpPost]
    public ActionResult Create(Assignment assignment, HttpPostedFileBase file)
    {
      if (file != null)
            {
                int byteCount = file.ContentLength;   <---Your file Size or Length
                .............
                .............//You can store file in database//
            }
     return RedirectToAction("Create");    
    }
    

    【讨论】:

    • 那接下来如何存入数据库呢?像这样? assignment.FileLocation = byteCount; db.Assignments.Add(赋值); db.SaveChanges();
    • @WeakestInCoding...上面显示的答案是保存文件的基本概念..以供完整使用...dotnet-tricks.com/Tutorial/mvc/…
    • @WeakestInCoding....请在这里参考这篇文章....bluelemoncode.com/post/2011/11/16/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2016-04-27
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多