【发布时间】:2018-11-20 00:20:13
【问题描述】:
我是 asp.net mvc 的新手。我需要有关如何使用 mvc4 从实体框架上传和检索图像的帮助。请给我一步一步的信息。
这是我的模型
BUser
public byte[] Image { get; set; }
控制器
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (file != null)
{
HMSEntities2 db = new HMSEntities2();
string ImageName = System.IO.Path.GetFileName(file.FileName);
byte[] y = System.Text.Encoding.UTF8.GetBytes(ImageName);
string physicalPath = Server.MapPath("~/images/" + ImageName);
// save image in folder
file.SaveAs(physicalPath);
//save new record in database
User newRecord = new User();
newRecord.Image = y;
db.Users.Add(newRecord);
db.SaveChanges();
}
//Display records
return RedirectToAction("Display","BPatient");
}
public ActionResult Display()
{
return View();
}
编辑视图
@using (Html.BeginForm("FileUpload","BPatient", FormMethod.Post, new{@class = "form-horizontal role='form' container-fluid enctype='multipart/form-data' " }))
<div class="form-group">
<input type="file" name="file" id="file" style="width: 100%;" />
<input type="submit" value="Upload" class="submit" />
</div>
【问题讨论】:
-
你能告诉我们你的尝试吗?
-
请看我更新的问题
-
stackoverflow.com/a/20825120/4523071 我正在关注本教程..每当我尝试上传图片时都会出现错误...即(HttpPostedFileBase 文件)为空
-
你用
@using( ... ){ the html here }包围表单?
标签: asp.net-mvc-4