在ASP.NET MVC5中查看和显示上传的文件?

时间:2014-12-29 09:25:57

标签: asp.net-mvc asp.net-mvc-5

任何人都可以帮我处理简单的电路板问题吗?

我使用ASP.NET MVC5创建了一个项目,该项目从用户那里获取输入文件,这是为我完成的。

在详细信息视图中,我想创建一个指向该文件的链接。

请任何帮助。

这是我的控制者:

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>

在模型下面

namespace WebApplication1.Models
{

    public enum Qualification
    {
        //Select,
        Diploma,
        Bacholer, Master, PhD
    }


    public enum Major
    {
        //Select,
        [Display(Name = "Software Engneering")]
        SoftwareEngneering,
        [Display(Name = "Information System")]
        InformationSystem,
        [Display(Name = "Information Technology")]
        InformationTechnology,

        [Display(Name = "Computer Engneering")]
        ComputerEngneering,
        [Display(Name = "Computer Science")]
        ComputerScience,
        [Display(Name = "Computer Security")]
        ComputerSecurity,
        [Display(Name = "Health Informatics")]
        HealthInformatics,
        [Display(Name = "Mangement Information System")]
        MangementInformationSystem,
        [Display(Name = "Telecommuncation Engneering")]
        TelecommuncationEngneering,
        [Display(Name = "Graphic Design")]
        GraphicDesign

    }
    public class Employee
    {

        [HiddenInput]
        public string Region { get; set; }
        public string id { get; set; }







        [Required]
        public string Name { get; set; }

        [Required]
        public string Position { get; set; }



        [DisplayFormat(NullDisplayText = "Select")]
        [Required]
        public Qualification? Qualification { get; set; }
        [DisplayFormat(NullDisplayText = "Select")]
        [Required]
        public Major? Major { get; set; }


        public int Experience { get; set; }


        [DataType(DataType.Date)]
        public DateTime JoinedDate { get; set; }


        public string Extension { get; set; }

        public string Mobile { get; set; }






        [Display(Name = "Office#")]

        public string OfficeNo { get; set; }




        [Display(Name = "Work Achivement")]

        [DataType(DataType.MultilineText)]
        public string Work_Achivement { get; set; }

        [NotMapped]
        [Display(Name = "C.V")]
        [DataType(DataType.Upload)]

        public HttpPostedFileBase CV { get; set; }
        [NotMapped]
        [Display(Name = "Personal Photo")]
        [DataType(DataType.Upload)]
        public HttpPostedFileBase PersonalPhoto { get; set; }



    }

}

非常感谢

1 个答案:

答案 0 :(得分:1)

你可以这样做:

@{
   var name = Model.CV.FileName;
   var link = @Url.Content("~/Content/CVs/") + name.Replace(" ", "%20") + ".pdf";
}

<td>
<a href="@link" >@name</a>
</td>

第一个块用于编写c#代码,然后我们使用html中的变量。

另一种方式是:

 <td>
    <a href="@(Url.Content("~/Content/CVs/"+name))">@name</a>
  </td>

UPDATE:

您还需要在模型中保存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; }

并在表中添加列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>