【问题标题】:Get binary from uploaded file (image) in ASP.NET MVC在 ASP.NET MVC 中从上传的文件(图像)中获取二进制文件
【发布时间】:2010-10-16 00:53:01
【问题描述】:

我正在使用以下代码:

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

我不想将文件保存到文件系统,而是想从传入文件中提取二进制数据,以便可以将图像提交到我的数据库。我可以对我的代码进行哪些更改来支持这一点?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 asp.net-4.0 uploading image-uploading


    【解决方案1】:

    也许在你的解决方案中试试这个 sn-p:

    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.
    

    【讨论】:

      猜你喜欢
      • 2011-01-31
      • 2018-09-04
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多