C# 我想上传文件在我的附件形式使用mvc5
我必须用上传文件功能创建表单。我使用.NET framework asp.NET mvc5和C作为语言。但我找不到我表格的教程。我尽力了,我真的需要你们的帮助。我将在这里添加我的模型、视图和控制器 我想知道如何创建上传文件功能。我试了那么多,但找不到路。我真的需要帮助 型号C# 我想上传文件在我的附件形式使用mvc5,c#,.net,file-upload,asp.net-mvc-5,C#,.net,File Upload,Asp.net Mvc 5,我必须用上传文件功能创建表单。我使用.NET framework asp.NET mvc5和C作为语言。但我找不到我表格的教程。我尽力了,我真的需要你们的帮助。我将在这里添加我的模型、视图和控制器 我想知道如何创建上传文件功能。我试了那么多,但找不到路。我真的需要帮助 型号 using System.Collections.Generic; namespace PMSWebApplication.Models.DomainModels { public class Attachment
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; }
}
}
使用System.Collections.Generic;
命名空间PMSWebApplication.Models.DomainModels
{
公共类附件
{
公共附件()
{
BugFixes=newHashSet();
Updates=newhashset();
}
公共int Id{get;set;}
公共int ProjectId{get;set;}
公共项目{get;set;}
公共字符串用户标识{get;set;}
公共应用程序用户{get;set;}
公共字符串文件名{get;set;}
公共字符串上载文件{get;set;}
//公共字符串类型{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection错误修复{get;set;}
公共虚拟ICollection更新{get;set;}
}
}
控制器
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);
}
}
}
使用System.Data.Entity;
使用System.Threading.Tasks;
Net系统;
使用System.Web.Mvc;
使用PMSWebApplication.Models;
使用PMSWebApplication.Models.DomainModels;
使用Microsoft.AspNet.Identity.EntityFramework;
使用Microsoft.AspNet.Identity;
命名空间PMSWebApplication.Controllers
{
公共类附件控制器:控制器
{
私有ApplicationDbContext db=新ApplicationDbContext();
//获取:附件
公共异步任务索引(/*HttpPostedFileBase文件*/)
{
var attachments=db.attachments.Include(a=>a.Project.Include)(a=>a.User);
返回视图(wait attachments.ToListAsync());
}
//获取:附件/详细信息/5
公共异步任务详细信息(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
附件附件=等待db.Attachments.FindAsync(id);
如果(附件==null)
{
返回HttpNotFound();
}
返回视图(附件);
}
//获取:附件/创建
公共操作结果创建()
{
var users=newusermanager(newuserstore(db)).users;
ViewBag.ProjectId=新选择列表(db.Projects,“Id”,“ProjectName”);
ViewBag.UserId=新的选择列表(用户,“Id”、“电子邮件”);
返回视图();
}
//发布:附件/创建
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息请参见http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(Include=“Id,ProjectId,UserId,FileName,UploadFile,Type,Description”)]附件)
{
if(ModelState.IsValid)
{
db.Attachments.Add(附件);
等待db.saveChangesSync();
返回操作(“索引”);
}
var users=newusermanager(newuserstore(db)).users;
ViewBag.ProjectId=新选择列表(db.Projects,“Id”,“ProjectName”,attachment.ProjectId);
ViewBag.UserId=新的选择列表(用户,“Id”,“电子邮件”,附件.UserId);
返回视图(附件);
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
db.Dispose();
}
基地。处置(处置);
}
}
}
查看索引
@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 IEnumerable
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
附件
@*@使用(Html.BeginForm(“Upload”、“Upload”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
*@
@**@
@*}*@
@ActionLink(“创建新附件”,“创建”,空,新{@class=“btn btn info”})
@DisplayNameFor(model=>model.Project.ProjectName)
@DisplayNameFor(model=>model.User.UserLevel)
@Html.DisplayNameFor(model=>model.FileName)
@DisplayNameFor(model=>model.FileType)
@DisplayNameFor(model=>model.Type)
@DisplayNameFor(model=>model.Description)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Project.ProjectName)
@DisplayFor(modelItem=>item.User.UserLevel)
@DisplayFor(modelItem=>item.FileName)
@DisplayFor(modelItem=>item.FileType)
@DisplayFor(modelItem=>item.Type)
@DisplayFor(modelItem=>item.Description)
@ActionLink(“编辑”,“编辑”,新建{id=item.id},新建{@class=“btn btn primary btn sm”})|
@ActionLink(“Details”,“Details”,new{id=item.id},new{@class=“btn btn info btn sm”})|
@ActionLink(“Delete”,“Delete”,new{id=item.id},new{@class=“btn btn warning btn sm”})
}
视图创建
@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")
}
@model pmswebaplication.Models.DomainModels.Attachment
@{
ViewBag.Title=“创建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
附件
@Html.ValidationSummary(true,“,new{@c
// 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);
}
}