我想在下载视图中显示数据库的内容(员工视图),并希望在数据表的末尾提供下载链接。
当前,我只能从AppData / Uploads文件下载文件,而不是将其链接到数据库中的增值数据,我还有其他数据存储在数据库中,我需要在视图中返回该文件以及下载选项文件。我的路径是字符串,并且设法获得了下载选项,但可悲的是,它只能到达仅包含文件名和链接的应用程序数据文件夹。如果能够做到,那将是惊人的
因此,我尝试存储byte []并执行HttpPostedFileBase,但是当尝试将其构建到我的“上传”视图中时,遇到了很多转换问题以及其他无法解决的问题。
我需要收集其他数据,而不仅仅是提供文件链接
员工视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectUploadJQuery.Models
{
public class Employee
{
public int Id
{
get;
set;
}
public string FullName
{
get;
set;
}
public string Skills
{
get;
set;
}
public string EmailID
{
get;
set;
}
public string ContactNo
{
get;
set;
}
public int PhoneNumber
{
get;
set;
}
public string Position
{
get;
set;
}
public string FileName
{
get;
set;
}
public string FileDescription
{
get;
set;
}
public string nadex
{
get;
set;
}
public int Preferred
{
get;
set;
}
public int ContactMe
{
get;
set;
}
public string QITraining
{
get;
set;
}
public string HospitalSite
{
get;
set;
}
public string AreaOfInterest
{
get;
set;
}
public string ProjectCategory
{
get;
set;
}
public string Aims
{
get;
set;
}
public string Stage
{
get;
set;
}
public string Membership
{
get;
set;
}
public DateTime CompletitionDate
{
get;
set;
}
public HttpPostedFileBase Resume
{
get;
set;
}
public Byte[] File
{
get;
set;
}
public Candidate Candidate { get; set; }
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using ProjectUploadJQuery.Models;
using System.DirectoryServices.AccountManagement;
using ProjectUploadJQuery.Helpers;
using System.Globalization;
using System.Net;
namespace ProjectUploadJQuery.Controllers
{
public class FileUploadController : Controller
{
private EFORMSDevelopmentEntities db = new EFORMSDevelopmentEntities ();
// GET: /FileUpload which is the form to collect data and upload the file/
[HttpGet]
public ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(Employee employee)
{
using (EFORMSDevelopmentEntities entity = new EFORMSDevelopmentEntities())
{
var candidate = new Candidate()
{
ContactNo = employee.ContactNo,
EmailID = employee.EmailID,
FullName = employee.FullName,
Position = employee.Position,
Resume = SaveToPhysicalLocation(employee.Resume),
Skills = employee.Skills,
CreatedOn = DateTime.Now,
FileName = employee.FileName,
FileDescription = employee.FileDescription,
nadex = employee.nadex,
HospitalSite = employee.HospitalSite,
Preferred = employee.Preferred,
ContactMe = employee.ContactMe,
PhoneNumber = employee.PhoneNumber,
QITraining = employee.QITraining,
Membership = employee.Membership,
AreaOfInterest = employee.AreaOfInterest,
ProjectCategory = employee.ProjectCategory,
Aims = employee.Aims,
Stage = employee.Stage,
CompletitionDate = employee.CompletitionDate,
File = employee.File
};
entity.Candidates.Add(candidate);
entity.SaveChanges();
}
ModelState.Clear();
return View(employee);
}
private object GetEmployee(Employee employee)
{
throw new NotImplementedException();
}
//view the uploaded files and download
public ActionResult Downloads()
{
Employee employee = new Employee();
var dir = new System.IO.DirectoryInfo(Server.MapPath("~/App_Data/Uploads/"));
System.IO.FileInfo[] fileNames = dir.GetFiles("*.*"); List<string> items = new List<string>();
foreach (var file in fileNames)
{
items.Add(file.Name);
}
return View(items);
}
public FileResult Download(string ImageName)
{
var FileVirtualPath = "~/App_Data/Uploads/" + ImageName;
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
public Models.Employee GetUserDetails(Models.Employee employee)
{
employee.nadex = User.Identity.Name.Replace(@"CYMRU\", string.Empty);
employee.FullName = UserPrincipal.Current.DisplayName;
try
{
using (PrincipalContext contextP = new PrincipalContext(ContextType.Domain))
{
//PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrinciple = UserPrincipal.FindByIdentity(contextP, User.Identity.Name);
if (userPrinciple != null)
{
employee.FullName = userPrinciple.DisplayName;
int removeBracketsLocation = employee.FullName.IndexOf("(") - 1;
employee.FullName = employee.FullName.Substring(0, removeBracketsLocation);
employee.FullName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(employee.FullName.ToLower());
}
}
}
catch
{
Redirect("~/AccessDenied/ErrorLoadingPage");
}
employee.nadex = User.Identity.Name.Replace(@"CYMRU\", string.Empty);
return employee;
}
/// <summary>
/// Save Posted File in Physical path and return saved path to store in a database
/// /// </summary>
/// <param name="file"></param>
/// <returns></returns>
private string SaveToPhysicalLocation(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
//var fileName = Guid.NewGuid() + Path.GetFileName(file.FileName);
var fileName =Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
file.SaveAs(path);
return path;
}
return string.Empty;
}
}
}
下载视图
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
table, th {
border: 1px solid grey;
padding: 10px;
}
thead {
background-color: skyblue;
color: white;
border: 1px solid grey;
}
</style>
@model List<string>
<h2>Downloads</h2>
<table class="table">
<tr>
<th>File Name</th>
<th>Link</th>
</tr>
@for (var i = 0; i <= Model.Count - 1; i++)
{
<tr>
<td> @Model[i].ToString() </td>
<td> @Html.ActionLink("Download", "Download", new { ImageName = @Model[i].ToString() }) </td>
</tr>
}
</table>
上传视图
@using System.Web.Mvc
@model ProjectUploadJQuery.Models.Employee
@{
ViewBag.Title = "Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<head>
<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Apply').prop('disabled', true);
$("#Resume").change(function () {
// Get uploaded file extension
var extension = $(this).val().split('.').pop().toLowerCase();
// Create array with the files extensions that we wish to upload
var validFileExtensions = ['doc', 'docx', 'pdf'];
//Check file extension in the array.if -1 that means the file extension is not in the list.
if ($.inArray(extension, validFileExtensions) == -1) {
alert("Sorry!! Upload only 'doc', 'docx', 'pdf' file")
// Clear fileuload control selected file
$(this).replaceWith($(this).val('').clone(true));
//Disable Submit Button
$('#Apply').prop('disabled', true);
}
else {
// Check and restrict the file size to 128 KB.
if ($(this).get(0).files[0].size > (131072)) {
alert("Sorry!! Max allowed file size is 128 kb");
// Clear fileuload control selected file
$(this).replaceWith($(this).val('').clone(true));
//Disable Submit Button
$('#Apply').prop('disabled', true);
}
else {
//Enable Submit Button
$('#Apply').prop('disabled', false);
}
}
});
//$("#Apply").click(function () {
// if ($("#FirstName").val() = null || $("#LastName").val() || $("#Skills").val() || $("#EmailID").val() || $("#ContactNo").val() || $("#Resume").val()) {
// alert("Please fill out required fields(*)");
// return false;
// }
// return true;
//});
});
</script>
</head>
@using (Html.BeginForm("Upload", "FileUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('p').hide();
$("#hide").click(function () {
$("p").hide();
});
$("#show").click(function () {
$("p").show();
return true;
});
});
</script>
<script>
$(document).ready(function () {
$('pp').hide();
$("#hidecommentA").click(function () {
$("pp").hide();
});
$("#hidecommentB").click(function () {
$("pp").hide();
});
$("#showcommentB").click(function () {
$("pp").show();
return true;
});
});
</script>
<script>
$(document).ready(function () {
$('ppp').hide();
$("#hidecommentBevan").click(function () {
$("ppp").hide();
});
$("#hidecommentQ").click(function () {
$("ppp").hide();
});
$("#showcommentO").click(function () {
$("ppp").show();
return true;
});
});
</script>
<script>
$(document).ready(function () {
$('pppp').hide();
$("#Specialtyhide").click(function () {
$("pppp").hide();
});
$("#IQThide").click(function () {
$("pppp").hide();
});
$("#Othershow").click(function () {
$("pppp").show();
return true;
});
});
</script>
<script>
$(document).ready(function () {
$('ppppp').hide();
$("#CompletedHide").click(function () {
$("pppp").hide();
});
$("#InCompleteShow").click(function () {
$("ppppp").show();
return true;
});
});
</script>
<script type="text/javascript">
$(function () { // will trigger when the document is ready
$('.datepicker').datepicker({
format: "dd/mm/yyyy", todayHighlight: true, autoclose: true
});
});
</script>
<style>
table, th {
border: 1px solid grey;
padding: 10px;
}
thead {
background-color: skyblue;
color: white;
border: 1px solid grey;
}
</style>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css" />
<div class="panel panel-info">
<div class="panel-heading">
Members and Clinician Details
</div>
<div class="panel-body">
<!--PANEL 1-->
<div class="row jm5pxPadding">
<div class="col-md-3">
<label for="Candidate_Name" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">1</span> Name</label>
@Html.TextBoxFor(m => m.FullName, new { @Value = "Michelle ", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", required = "required", style = "width:250px" })
</div>
<div class="col-md-3">
<label for="Candidate_Nadex" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">1</span> Nadex</label>
@Html.TextBoxFor(m => m.nadex, new { @Value = User.Identity.Name.Replace(@"CYMRU\", string.Empty), @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", required = "required", style = "width:250px" })
</div>
<div class="col-md-3">
<label for="Candidate_Position" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">2</span> Job Title</label>
@Html.TextBoxFor(m => m.Position, new { @Value = "Developer", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Full Job Title", required = "required", style = "width:250px" })
</div>
<div class="col-md-3">
<label for="Candidate_HospitalSite" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">3</span> Hospital Site</label>
@Html.TextBoxFor(m => m.HospitalSite, new { @Value = "UK", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Start Typing Site", required = "required", style = "width:250px" })
</div>
</div>
<div class="row jm5pxPadding">
<div class="col-md-3">
<label for="Candidate_EmailID" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">4</span> Email Address</label>
@Html.TextBoxFor(m => m.EmailID, new { @Value = "", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Enter email address", required = "required", style = "width:250px" })
</div>
<div class="col-md-3">
<label for="Candidate_ContactNo" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">5</span> Contact Number</label>
@Html.TextBoxFor(m => m.ContactNo, new { @Value = "", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Enter phone number", required = "required", style = "width:250px" })
</div>
<div class="col-md-3">
<label for="Candidate_PhoneNumber" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">6</span> Alterative Contact Number</label>
@Html.TextBoxFor(m => m.PhoneNumber, new { @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Enter phone number", required = "required", style = "width:250px" })
</div>
</div>
<div class="col-md-10">
<label for="Candidate_ContactMe" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">7</span> Do you wish to be contacted by other QI Hub Members?</label>
@Html.RadioButtonFor(m => m.ContactMe, "1", new
{
id = "show"
})
<label for="ProjectUploadJQuery_1" class="control-label jmLabelUnbold ">Yes</label>
@Html.RadioButtonFor(m => m.ContactMe, "2", new
{
id = "hide"
})
<label for="ProjectUploadJQuery_1" class="control-label jmLabelUnbold ">No</label>
</div>
<br>
<br>
<div class="col-md-10">
<p>
<label for="Candidate_Preferred" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">7a</span> Please select your preferred method of contact</label>
@Html.RadioButtonFor(m => m.Preferred, "1", new
{
})
<label for="ProjectUploadJQuery_1" class="control-label jmLabelUnbold ">Email</label>
@Html.RadioButtonFor(m => m.Preferred, "2", new
{
})
<label for="ProjectUploadJQuery_1" class="control-label jmLabelUnbold ">Phone</label>
</p>
</div>
</div>
</div>
<!--PANEL 3 - QUESTIONS-->
<div class="panel panel-info">
<div class="panel-heading">
Training and Education
</div>
<div class="panel-body">
<div class="row jm5pxPadding">
<div class="col-md-5"><span class="badge jmMiddleAlignBadge">8</span> Please provide details of Quality Improvement Training</div>
<div class="col-md-7">
@Html.RadioButtonFor(m => m.QITraining, "IA", new { id = "hidecommentA", groupname = "QITraining" })
<label for="ProjectUploadJQuery_IA" , class="control-label jmLabelUnbold ">IA (or equivalent)</label>
@Html.RadioButtonFor(m => m.QITraining, "IQT", new { id = "hidecommentB", groupname = "QITraining" })
<label for="ProjectUploadJQuery_IQT" , class="control-label jmLabelUnbold ">IQT'S</label>
@Html.RadioButtonFor(m => m.QITraining, "Other", new { id = "showcommentB", groupname = "QITraining" })
<label for="ProjectUploadJQuery_Other" , class="control-label jmLabelUnbold ">Other</label>
</div>
</div>
<pp>
<div class="col-md-offset-5">
@Html.TextBoxFor(m => m.QITraining, new { @placeholder = "Please specify any further training", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", style = "width:500px" })
</div>
</pp>
<div class="row jm5pxPadding">
<div class="col-md-5"><span class="badge jmMiddleAlignBadge">9</span> Are you a member of any other QI networks </div>
<div class="col-md-7">
@Html.RadioButtonFor(m => m.Membership, "Bevan", new { id = "hidecommentBevan", groupname = "Aims" })
<label for="ProjectUploadJQuery_IA" , class="control-label jmLabelUnbold ">Bevan</label>
@Html.RadioButtonFor(m => m.Membership, "Q", new { id = "hidecommentQ", groupname = "Aims" })
<label for="ProjectUploadJQuery_IQT" , class="control-label jmLabelUnbold ">Q</label>
@Html.RadioButtonFor(m => m.Membership, "Other", new { id = "showcommentO", groupname = "Aims" })
<label for="ProjectUploadJQuery_Other" , class="control-label jmLabelUnbold ">Other</label>
</div>
</div>
<ppp>
<div class="col-md-offset-5">
@Html.TextBoxFor(m => m.Membership, new { @placeholder = "Please specify any other memberships", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", style = "width:500px" })
</div>
</ppp>
<div class="row jm5pxPadding">
<div class="col-md-5"><span class="badge jmMiddleAlignBadge">10</span> Areas of interest </div>
<div class="col-md-7">
@Html.RadioButtonFor(m => m.AreaOfInterest, "Specialty", new { id = "Specialtyhide", groupname = "AreaOfInterest" })
<label for="ProjectUploadJQuery_IA" , class="control-label jmLabelUnbold "> Specialty</label>
@Html.RadioButtonFor(m => m.AreaOfInterest, "Topic", new { id = "IQThide", groupname = "AreaOfInterest" })
<label for="ProjectUploadJQuery_IQT" , class="control-label jmLabelUnbold ">Topic</label>
@Html.RadioButtonFor(m => m.AreaOfInterest, "Other", new { id = "Othershow", groupname = "AreaOfInterest" })
<label for="ProjectUploadJQuery_IQT" , class="control-label jmLabelUnbold ">Other</label>
</div>
</div>
<pppp>
<div class="col-md-offset-5">
@Html.TextBoxFor(m => m.AreaOfInterest, new { @placeholder = "Please specify any other areas of interest", @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", style = "width:500px" })
</div>
</pppp>
</div>
</div>
<!--PANEL 3 - QUESTIONS-->
<div class="panel panel-info">
<div class="panel-heading">
Quality and Innovation Project
</div>
<div class="panel-body">
<div class="row jm5pxPadding">
<div class="col-md-5">
<label for="Candidate_ProjectCategory" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">11</span> Please select a category for your Project</label>
</div>
<div class="col-md-5">
@Html.TextBoxFor(m => m.ProjectCategory, new { @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "DROPDOWN", required = "required", style = "width:250px" })
</div>
</div>
<div class="row jm5pxPadding">
<div class="col-md-5">
<label for="Candidate_Aims" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">12</span> Please provide details of the aims of your Project</label>
</div>
<div class="col-md-5">
@Html.TextBoxFor(m => m.Aims, new { @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Please specify", required = "required", style = "width:500px" })
</div>
</div>
<div class="row jm5pxPadding">
<div class="col-md-5">
<label for="Candidate_FileName" class="control-label jmLabelUnbold "><span class="badge jmMiddleAlignBadge">00</span> File Name</label>
</div>
<div class="col-md-5">
@Html.TextBoxFor(m => m.FileName, new { @class = "jmHalfWidth form-control dateP jmBottomMargin input-sm inputmask-99-99-9999", @placeholder = "Please specify", required = "required", style = "width:500px" })
</div>
</div>
<div class="row jm5pxPadding">
<div class="col-md-5"><span class="badge jmMiddleAlignBadge">13</span> Completion Stage </div>
<div class="col-md-7">
@Html.RadioButtonFor(m => m.Stage, "1", new { id = "CompletedHide", groupname = "Stage" })
<label for="ProjectUploadJQuery_Stage" , class="control-label jmLabelUnbold "> Completed</label>
@Html.RadioButtonFor(m => m.Stage, "2", new { id = "InCompleteShow", groupname = "Stage" })
<label for="ProjectUploadJQuery_Stage" , class="control-label jmLabelUnbold ">In Progress</label>
<ppppp>
<div class="row jm5pxPadding">
@Html.TextBoxFor(m => m.CompletitionDate, new { id = "ConpletionDate", @class = "form-control datepicker", @placeholder = "Please estimate completition date", style = "width:250px" })
</div>
</ppppp>
</div>
</div>
<div class="row jm5pxPadding">
<div class="col-md-5"><span class="badge jmMiddleAlignBadge">14</span> Please upload your file </div>
@Html.TextBoxFor(m => m.Resume, new { type = "file" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<input class="btn btn-primary" type="submit" value="Apply ">
<input class="btn btn-primary" type="reset" value="Reset form ">
</div>
</div>
}
@Html.ActionLink("Documents", "Downloads")