Asp.net mvc 是否在ASP.NET MVC5中查看和显示上载的文件?
有人能帮我解决这个简单的电路板问题吗 我用ASP.NET MVC5创建了一个项目,该项目从用户处获取输入文件,这是为我完成的 在“详细信息”视图中,我要创建指向该文件的链接 请帮忙 这是我的控制器:Asp.net mvc 是否在ASP.NET MVC5中查看和显示上载的文件?,asp.net-mvc,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 5,有人能帮我解决这个简单的电路板问题吗 我用ASP.NET MVC5创建了一个项目,该项目从用户处获取输入文件,这是为我完成的 在“详细信息”视图中,我要创建指向该文件的链接 请帮忙 这是我的控制器: public ActionResult Details(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadR
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
// to view the uploaded file
return View(employee);
}
//POST Details
// GET: Employees/Create
public ActionResult Create()
{
return View();
}
// POST: Employees/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,Region,Name,Position,Qualification,Major,Experience,JoinedDate,Extension,Mobile,OfficeNo,Work_Achivement")] Employee employee, HttpPostedFileBase CV)
{
if (ModelState.IsValid)
{
if (CV != null && CV.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(CV.FileName);
// store the file inside ~/Content/LearnObject-Repository folder
var path = Path.Combine(Server.MapPath("~/Content/CVs"), fileName);
CV.SaveAs(path);
var fileNameToSaveInDB = @"~/Content/CVs/" + fileName;
}
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
// GET: Employees/Edit/5
public ActionResult Edit(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
以下是我对细节的看法:
@model WebApplication1.Models.Employee
@{
ViewBag.Title = "Details";
}
<h2 style="font-family:'Times New Roman', Times, serif">Employee Details</h2>
<div>
<hr />
<table class="table table-bordered">
<tr>
<th width="250" >
@Html.DisplayNameFor(model => model.Region)
</th>
<td>
@Html.DisplayFor(model => model.Region)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<td>
@Html.DisplayFor(model => model.Name)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Position)
</th>
<td>
@Html.DisplayFor(model => model.Position)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Qualification)
</th>
<td>
@Html.DisplayFor(model => model.Qualification)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Major)
</th>
<td>
@Html.DisplayFor(model => model.Major)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Experience)
</th>
<td>
@Html.DisplayFor(model => model.Experience)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.JoinedDate)
</th>
<td>
@Html.DisplayFor(model => model.JoinedDate)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Extension)
</th>
<td>
@Html.DisplayFor(model => model.Extension)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Mobile)
</th>
<td>
@Html.DisplayFor(model => model.Mobile)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.OfficeNo)
</th>
<td>
@Html.DisplayFor(model => model.OfficeNo)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Work_Achivement)
</th>
<td>
@Html.DisplayFor(model => model.Work_Achivement)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.CV)
</th>
<td>
@{
var name = Model.CV.FileName;
var link = @Url.Content("~/Content/CVs/") + name.Replace(" ", "%20") + ".pdf";
string tag = "<a href=" + link + ">" + name + "</a>";
//var x = fname.ToString();
}
</td>
</tr>
</table>
</div>
@{
var diffrence = DateTime.UtcNow - Model.JoinedDate;
var Total_Years = (diffrence.TotalDays / 365);
var KSAUHSExperience = Math.Round(Total_Years,1);
var Years = Math.Round(Model.Experience + Total_Years);
var months = Math.Round(Years % 12);
@*<text>Total Experience: </text>@Html.DisplayTextFor(model => Years)<text> Years and </text> @Html.DisplayTextFor(model => months)<text> month </text>; <br />*@
<p style="font-family:'Times New Roman', 'Times', 'serif';font-size:medium">Total Experience:@Html.DisplayTextFor(model => Years)Years and @Html.DisplayTextFor(model => months) Monthes</p>
<p style="font-family:'Times New Roman', 'Times', 'serif';font-size:medium"> Experience at ksau-hs @Html.DisplayTextFor(model => KSAUHSExperience)</p>
@*<text> experience in ksau-hs </text> @Html.DisplayTextFor(model => KSAUHSExperience)*@
}
<p>
@Html.ActionLink("Back to List", "Index")
</p>
非常感谢您可以这样做:
@{
var name = Model.CV.FileName;
var link = @Url.Content("~/Content/CVs/") + name.Replace(" ", "%20") + ".pdf";
}
<td>
<a href="@link" >@name</a>
</td>
与:
并在表中添加列,即CV
,用于存储文件名
然后在保存前设置CV文件名并将其保存到数据库中:
if (CV != null && CV.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(CV.FileName);
// store the file inside ~/Content/LearnObject-Repository folder
var path = Path.Combine(Server.MapPath("~/Content/CVs"), fileName);
CV.SaveAs(path);
var fileNameToSaveInDB = @"~/Content/CVs/" + fileName;
employee.CV = CV.FileName;
}
db.Employees.Add(employee);
然后,鉴于:
<td>
<a href="@(Url.Content("~/Content/CVs/"+Model.CV))">@Model.CV</a>
</td>
我可以知道您上传的文件类型吗?var name=model.CV.FileName的问题;对象引用未设置为对象的实例。@Ehsanit表示它CV
为空。非常感谢您的帮助,但是CV不是空的。我已经上载了文件。文件名有问题,如何获取文件名?在bedug模式下检查,Model.CV将为空
[NotMapped]
[Display(Name = "C.V")]
[DataType(DataType.Upload)]
public HttpPostedFileBase CV { get; set; }
[NotMapped]
[Display(Name = "C.V")]
[DataType(DataType.Upload)]
public string CV { get; set; }
if (CV != null && CV.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(CV.FileName);
// store the file inside ~/Content/LearnObject-Repository folder
var path = Path.Combine(Server.MapPath("~/Content/CVs"), fileName);
CV.SaveAs(path);
var fileNameToSaveInDB = @"~/Content/CVs/" + fileName;
employee.CV = CV.FileName;
}
db.Employees.Add(employee);
<td>
<a href="@(Url.Content("~/Content/CVs/"+Model.CV))">@Model.CV</a>
</td>