我已经创建了图片上传。唯一的问题是,当我上传图片时,上传的图片没有显示在剃须刀视图中(后端编辑器),所以当有人来编辑这个信息时,它显示为“选择文件”即使在那里上传是一个存储在数据库中并显示在前端的..
这是我的代码: - 控制器
public class FileUploadAPIController : Controller
{
private readonly MainDbContext _db;
public FileUploadAPIController()
{
_db = new MainDbContext();
}
[HttpPost]
public ActionResult Save(IEnumerable<HttpPostedFileBase> files, int id)
{
if (files != null)
{
foreach (var file in files)
{
var fileName = Path.GetFileName(file.FileName);
var folder = Server.MapPath("~/Images/ManagerPictures/" + id);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var physicalPath = Path.Combine(folder, fileName);
file.SaveAs(physicalPath);
var Manager = _db.Managers.Find(id);
Manager.ManagerPicture = "/manage/Images/ManagerPictures/" + id + "/" + fileName;
_db.SaveChanges();
}
}
return Content("");
}
public ActionResult Remove(string[] fileNames, int id)
{
var Manager = _db.Managers.Find(id);
Manager.ManagerPicture = null;
_db.SaveChanges();
return Content("");
}
private IEnumerable<string> GetFileInfo(IEnumerable<HttpPostedFileBase> files)
{
return
from a in files
where a != null
select string.Format("{0} ({1} bytes)", Path.GetFileName(a.FileName), a.ContentLength);
}
}
.cshtml:
<div id="example">
<h1>Manager Photo:</h1>
@(Html.Kendo().Upload()
.Name("Files")
.Multiple(false)
.Async(a => a
.Save("Save", "FileUploadApi", new { id = Model.Manager.Id })
.Remove("Remove", "FileUploadApi", new { id = Model.Manager.Id })
.AutoUpload(true))
)
<img src="@Url.Content("~/manage/Images/ManagerPictures/")" />
<img src="@Url.Action("GetFileInfo", "FileUploadApi", new { id = Model.Manager.Id })" />
</div>
有没有人有任何提示/帮助让我朝着正确的方向前进? 提前谢谢。
注意:在cshtml的上方你可以看到我试图显示图像但不完全确定这是否是正确的方法..
答案 0 :(得分:0)
<img src="@Model.Manager.ManagerPicture" />
在Razor视图中..但感谢每个人的输入