保存/检索图像.ASP.NET MVC
本文关键字:NET MVC ASP 图像 检索 保存 | 更新日期: 2023-09-27 18:26:26
我正在尝试显示以前在视图上加载的图像,但没有成功。图片上传,这是肯定的。到目前为止,我已经写了这样的东西:
@if(Model.AttachedInformation.Count > 0)
{
<div id="gallery">
@foreach(var path in Model.AttachedInformation)
{
<img src="@path" alt="default_description" title="some_title" />
}
</div>
}
AttachedInformation
只是一个ICollection<String>
对象。
但这只给了我图像的边界。此外,我检查了@path
变量,它确实保留了完整的文件路径。
建议不胜感激!谢谢!
编辑:控制器
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Employee employee, IEnumerable<HttpPostedFileBase> files)
{
if(ModelState.IsValid)
{
var filepath = String.Empty;
foreach(var file in files)
{
if(null != file && file.ContentLength > 0)
{
filepath = Path.Combine(
HttpContext.Server.MapPath("~/Content/Images/Uploads"),
Path.GetFileName(file.FileName)
);
file.SaveAs(filepath);
employee.AttachedInformation.Add(filepath);
}
}
this.repository.Add(employee);
return Redirect("/");
}
else
{
return View(employee);
}
}
型
AbstractEntity
保留Version
和ID
属性。
[Serializable]
public class Employee : AbstractEntity<Employee>, IAggregateRoot
{
public Employee()
{
this.AttachedInformation = new HashSet<String>();
}
public virtual String FirstName { get; set; }
public virtual String MiddleName { get; set; }
public virtual String LastName { get; set; }
public virtual String SSN { get; set; }
public virtual String EmployeeNumber { get; set; }
public virtual String TradeUnionNumber { get; set; }
public virtual String Department { get; set; }
public virtual String Post { get; set; }
public virtual DateTime DateOfHire { get; set; }
public virtual DateTime DateOfBirth { get; set; }
public virtual ICollection<String> AttachedInformation { get; set; }
}
视图
视图包含多个名称为 files
的input
,如下所示:
<input type="file" name="files" id="additional" class=" " accept="image/jpeg,image/png,image/gif" />
原始网页
<div id="gallery">
<img src="D:'Programming.Projects'BOA'branches'BOA.PresentationLayer'Content'Images'Uploads'Desert.jpg" alt="default_description" title="some_title" />
<img src="D:'Programming.Projects'BOA'branches'BOA.PresentationLayer'Content'Images'Uploads'Hydrangeas.jpg" alt="default_description" title="some_title" />
<img src="D:'Programming.Projects'BOA'branches'BOA.PresentationLayer'Content'Images'Uploads'Jellyfish.jpg" alt="default_description" title="some_title" />
<img src="D:'Programming.Projects'BOA'branches'BOA.PresentationLayer'Content'Images'Uploads'Koala.jpg" alt="default_description" title="some_title" />
</div>
赏金
我需要的是得到一个图解示例(有效(。谢谢!
HttpContext.Server.MapPath("~/Content/Images/Uploads")
以上将为您提供磁盘上的完整路径,然后将其保存到员工记录中。您应该只保存文件名,然后将图像来源到~/内容/图像/上传/
另一个注意事项是,您将所有图像按文件名保存到同一目录中。如果两个用户上传同名文件,则其中一个用户将覆盖另一个用户。每个员工都应该有自己的上传目录,或者你应该动态生成文件名。
好的。我找到了一个解决方案。也许这对某人有用。
var directory = "/Content/Images/Uploads/" + employee.SSN;
var path = String.Empty;
if(!Directory.Exists(Server.MapPath(directory)))
{
Directory.CreateDirectory(Server.MapPath(directory));
}
foreach(var file in files)
{
if(null != file && file.ContentLength > 0)
{
path = Path.Combine(directory + "/", Path.GetFileName(file.FileName));
file.SaveAs(Server.MapPath(path));
employee.AttachedInformation.Add(path);
}
}
这对我有用。欢迎更正。