【发布时间】:2016-12-21 12:22:25
【问题描述】:
这是我的控制器代码,用于将上传的图像保存在我的应用程序文件夹(“~/Content/Files”)中,并将路径存储在名为“imageupload”的数据库表中。
我的目标是使用我的数据库表中的路径显示存储在文件夹(“~/Content/Files”)中的所有图像。
[HttpPost]
public ActionResult Index(prop prop)
{
var db = new db_DotnetEntities();
HttpPostedFileBase file = Request.Files[0];
if (ModelState.IsValid)
{
var tbl = new imageupload();
if (file == null)
{
ModelState.AddModelError("File", "Please Upload file");
}
else if (file.ContentLength > 0)
{
int MaxContentLength = 1024 * 1024 * 3;
string[] allowedFileExtensions = { ".jpeg", ".png", ".pdf", "gif" };
if (!allowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ModelState.AddModelError("File", "Please File of type" + string.Join(",", allowedFileExtensions));
}
else if (file.ContentLength > MaxContentLength)
{
ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
}
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Files"), fileName);
tbl.imageurl = fileName;
db.imageuploads.Add(tbl);
db.SaveChanges();
file.SaveAs(path);
ModelState.AddModelError("File", "Image Sucessfully Uploaded");
}
}
return View();
}
这是我的模型
public class prop
{
public string fileimg { get; set; }
}
这是我的观点
@model imageMVC.Models.prop
@using (Html.BeginForm("index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary()
@Html.TextBoxFor(x=>x.fileimg,new{@type="File"})
<input type="submit" id="submit"/>
}
我正在使用 ADO 数据实体模型。
请告诉我如何编写代码来显示存储在 ("~/Content/Files") 中的所有图像。
提前致谢。
【问题讨论】:
-
return View(db.imageuploads.ToList()),在您看来,在<img src="" />元素中打印文件名?你试过什么?
标签: c# razor model-view-controller