从ASP.NET MVC中的上传文件(图像)获取二进制文件 [英] Get binary from uploaded file (image) in ASP.NET MVC
本文介绍了从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");
}
我想从传入的文件中提取二进制数据,而不是将文件保存到文件系统中,以便将映像提交到数据库中.我可以对我的代码进行哪些更改以支持此操作?
Instead of saving the file to the filesystem, I want to extract the binary data from the incoming file so I can commit the image to my database. What changes can I make to my code to support this?
推荐答案
也许在您的解决方案中尝试以下代码段:
Perhaps try this snippet in your solution:
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.
这篇关于从ASP.NET MVC中的上传文件(图像)获取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文