0

我用下面的代码:获得在ASP.NET MVC上传文件(影像)的二进制

<form action="" method="post" enctype="multipart/form-data"> 

    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file" /> 

    <input type="submit" /> 
</form> 

而且......

[HttpPost] 
public ActionResult Index(HttpPostedFileBase file) { 

    if (file.ContentLength > 0) { 
    var fileName = Path.GetFileName(file.FileName); 
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); 
    file.SaveAs(path); 
    } 

    return RedirectToAction("Index"); 
} 

取而代之的将文件保存到文件系统,我想从传入文件中提取二进制数据,这样我就可以将图像提交到我的数据库。我可以对我的代码进行哪些更改以支持此操作?

回答

3

也许尝试这个片段在您的解决方案:

byte[] imgData; 
using (BinaryReader reader = new BinaryReader(file.InputStream)) { 
    imgData = reader.ReadBytes(file.InputStream.Length); 
} 

//send byte array imgData to database, or use otherwise as required.