Link 在 MVC5 中上传文件?
Link to uploaded files in MVC5?
我两周前就遇到了这个问题,请帮忙。
我创建了一个带有 HttpPostedFileBase 字段的模型,用于将文件保存在文件夹中,它在创建视图中非常适合我。
现在我想在详情视图中对上传的文件做一个link。
我尝试了几种方法,但找不到解决方案。
请帮忙。
在模型中,我为文件定义了一些参数和以下内容
[NotMapped]
[Display(Name = "C.V")]
[DataType(DataType.Upload)]
public HttpPostedFileBase CV { get; set; }
在控制器中我做了以下操作
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/Content/LearnObject-Repository folder
var path = Path.Combine(Server.MapPath("~/Content/CVs"), fileName);
file.SaveAs(path);
var fileNameToSaveInDB = @"~/Content/CVs/" + fileName;
}
db.Employees.Add(employee);
db.SaveChanges();
谢谢,
有关您实际尝试过的内容的更多信息会有所帮助,但这里有几个简单的步骤:
在您的模型中添加新的 属性
[NotMapped]
public string FileUrl { get; set; }
在控制器中设置值
public ActionResult Details(int id = 0)
{
MyModel myModel = db.MyModel.Find(id);
myModel.FileUrl = ""; // <-- resolve link;
...
}
并在您的视图中添加 link
<div class="display-field">
<a href="@Html.DisplayFor(model => model.FileUrl)">Link to file</a>
</div>
我两周前就遇到了这个问题,请帮忙。
我创建了一个带有 HttpPostedFileBase 字段的模型,用于将文件保存在文件夹中,它在创建视图中非常适合我。
现在我想在详情视图中对上传的文件做一个link。
我尝试了几种方法,但找不到解决方案。
请帮忙。
在模型中,我为文件定义了一些参数和以下内容
[NotMapped]
[Display(Name = "C.V")]
[DataType(DataType.Upload)]
public HttpPostedFileBase CV { get; set; }
在控制器中我做了以下操作
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/Content/LearnObject-Repository folder
var path = Path.Combine(Server.MapPath("~/Content/CVs"), fileName);
file.SaveAs(path);
var fileNameToSaveInDB = @"~/Content/CVs/" + fileName;
}
db.Employees.Add(employee);
db.SaveChanges();
谢谢,
有关您实际尝试过的内容的更多信息会有所帮助,但这里有几个简单的步骤:
在您的模型中添加新的 属性
[NotMapped]
public string FileUrl { get; set; }
在控制器中设置值
public ActionResult Details(int id = 0)
{
MyModel myModel = db.MyModel.Find(id);
myModel.FileUrl = ""; // <-- resolve link;
...
}
并在您的视图中添加 link
<div class="display-field">
<a href="@Html.DisplayFor(model => model.FileUrl)">Link to file</a>
</div>