我正在尝试显示我之前在视图中加载的图像,但没有成功。 图片已上传,这是肯定的。到目前为止,我写过这样的话:
@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);
}
}
MODEL
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; }
}
查看
视图包含名称为input
的多个files
,如:
<input type="file" name="files" id="additional" class=" " accept="image/jpeg,image/png,image/gif" />
RAW HTML
<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>
BOUNTY
我需要的是获得一个插图示例(有效)。谢谢!
答案 0 :(得分:4)
HttpContext.Server.MapPath("~/Content/Images/Uploads")
以上内容将为您提供磁盘上的完整路径,然后将其保存到员工记录中。您应该保存文件的名称,然后将图像采集到〜/ Content / Images / Uploads /
另一个注意事项是您要按文件名将所有图像保存到同一目录。如果两个用户上传具有相同名称的文件,则会覆盖另一个文件。每个员工都应该有自己的上传目录,或者您应该动态生成文件名。
答案 1 :(得分:1)
确定。我找到了解决方案。也许它会对某人有用。
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);
}
}
这对我有用。我们赞赏更正。