C#MVC4ASP.net,如何将文件插入数据库存储(二进制)

C#MVC4ASP.net,如何将文件插入数据库存储(二进制),c#,asp.net,asp.net-mvc,asp.net-mvc-4,httppostedfilebase,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Httppostedfilebase,是否有人能指导我,如何将文件保存到我的数据库中,并在可能的情况下检索它, 我对这个C#和MVC4还是新手。 我的数据库分配包含一个属性callfilelocation,它是varBinary(MAX) 型号 查看 @使用(Html.BeginForm(“Create”、“Assignment”、FormMethod.Post、new{enctype=“multipart/formdata”})) { ... model.FileLocation,新的{type=“file”})%> model.

是否有人能指导我,如何将文件保存到我的数据库中,并在可能的情况下检索它, 我对这个C#和MVC4还是新手。 我的数据库分配包含一个属性callfilelocation,它是varBinary(MAX)

型号 查看
@使用(Html.BeginForm(“Create”、“Assignment”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
...
model.FileLocation,新的{type=“file”})%>
model.FileLocation)%%>
...
}

在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");    
}
[HttpPost]
公共操作结果创建(分配,HttpPostedFileBase文件)
{
如果(文件!=null)
{

int byteCount=file.ContentLength;这里需要更多的处理。上传的文件以
HttpPostedFileBase
的形式进入,而不是
byte[]
,因此需要从HttpPostedFileBase的输入流中获取
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");    
}
另外,该模型看起来很像实体对象。使用实体对象作为模型是一个非常糟糕的主意。请尝试制作一个中间模型,并使用它来呈现数据

编辑 在您看来,更改此选项:

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

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


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

那么接下来如何存储到数据库中?如下所示?assignment.FileLocation=byteCount;db.Assignments.Add(赋值);db.SaveChanges();@wealtestincoding…上面显示的答案是保存文件的基本概念..要完整使用…@wealtestincoding…请参阅这里的文章…输入不是有效的Base-64字符串,因为它包含一个非Base-64字符、两个以上的填充字符或填充字符中的非法字符。尝试此c时会发生此错误你知道是什么导致了这个错误吗?我的数据库属性是varbinary(MAX),模型是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");    
}
<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
<input type="file" id="file" name="file" />