[英]I want upload file in my attachment form using mvc5
我必须使用上传文件 function 创建表单。 我使用 .NET 框架 asp.net mvc5 和 C# 作为语言。 但我找不到表格的教程。 我尽力了,我真的需要你们的帮助。 我将在此处添加我的 model、视图和 controller。
我想知道如何创建上传文件 function。 我尝试了很多,但我找不到方法。 我真的需要帮助
Model
using System.Collections.Generic;
namespace PMSWebApplication.Models.DomainModels
{
public class Attachment
{
public Attachment()
{
BugFixes = new HashSet<BugFix>();
Updates = new HashSet<Update>();
}
public int Id { get; set; }
public int ProjectId { get; set; }
public Project Project { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public string FileName { get; set; }
public string UploadFile { get; set; }
//public string Type { get; set; }
public string Description { get; set; }
public virtual ICollection<BugFix> BugFixes { get; set; }
public virtual ICollection<Update> Updates { get; set; }
}
}
Controller
using System.Data.Entity;
using System.Threading.Tasks;
using System.Net;
using System.Web.Mvc;
using PMSWebApplication.Models;
using PMSWebApplication.Models.DomainModels;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;
namespace PMSWebApplication.Controllers
{
public class AttachmentsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Attachments
public async Task<ActionResult> Index(/*HttpPostedFileBase file*/)
{
var attachments = db.Attachments.Include(a => a.Project).Include(a => a.User);
return View(await attachments.ToListAsync());
}
// GET: Attachments/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Attachment attachment = await db.Attachments.FindAsync(id);
if (attachment == null)
{
return HttpNotFound();
}
return View(attachment);
}
// GET: Attachments/Create
public ActionResult Create()
{
var users = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db)).Users;
ViewBag.ProjectId = new SelectList(db.Projects, "Id", "ProjectName");
ViewBag.UserId = new SelectList(users, "Id", "Email");
return View();
}
// POST: Attachments/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 async Task<ActionResult> Create([Bind(Include = "Id,ProjectId,UserId,FileName,UploadFile,Type,Description")] Attachment attachment)
{
if (ModelState.IsValid)
{
db.Attachments.Add(attachment);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
var users = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db)).Users;
ViewBag.ProjectId = new SelectList(db.Projects, "Id", "ProjectName", attachment.ProjectId);
ViewBag.UserId = new SelectList(users, "Id", "Email", attachment.UserId);
return View(attachment);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
查看索引
@model IEnumerable<PMSWebApplication.Models.DomainModels.Attachment>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Attachments</h2>
@*@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,new { enctype="multipart/form-data"}))
{
<input type="file" name="file" />
<br/>*@
@*<input type="submit" value="Upload" class="btn btn-primary" />*@
@*}*@
<p>
@Html.ActionLink("Create New Attachment", "Create", null, new { @class = "btn btn-info" })
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Project.ProjectName)
</th>
<th>
@Html.DisplayNameFor(model => model.User.UserLevel)
</th>
<th>
@Html.DisplayNameFor(model => model.FileName)
</th>
<th>
@Html.DisplayNameFor(model => model.FileType)
</th>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Project.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserLevel)
</td>
<td>
@Html.DisplayFor(modelItem => item.FileName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FileType)
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-primary btn-sm" }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-info btn-sm" }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "btn btn-warning btn-sm" })
</td>
</tr>
}
</table>
查看-创建
@model PMSWebApplication.Models.DomainModels.Attachment
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Attachment</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProjectId, "ProjectId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProjectId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FileName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FileName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FileType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FileType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FileType, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
以下是来自工作站点的相关片段:
// cshtml
<input type="file" name="ReportFile" />
// viewmodel
public class RequestViewModel
{
public HttpPostedFileBase ReportFile { get; set; }
}
// controller method
public virtual ActionResult Upload(RequestViewModel req)
{
var fileName = Path.GetFileName(req.ReportFile.FileName);
using (var rdr = new BinaryReader(req.ReportFile.InputStream))
{
var content = rdr.ReadBytes(req.ReportFile.ContentLength);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.