asp.net - Asp.net核心上传文件并将文件url存储到数据库
问题描述
我的代码在一个新的单独测试项目中工作,但它不能与我现有的工作项目一起工作。我正在尝试将文件作为表单的一部分上传,将其存储在 wwwroot 的文件夹中,并将 URL 存储在数据库中。我在下面发布了代码。该程序编译并运行,但在按下创建按钮时不存储任何内容。任何帮助将不胜感激。
//模型
namespace PostProjectEvaluations.Web.Models
{
public partial class Projects
{
[Key]
public int ProjectId { get; set; }
[Required]
[StringLength(300)]
public string Name { get; set; }
[Required]
[StringLength(50)]
public string Manager { get; set; }
public string FilePath { get; set; }
}
public class ProjectsVM
{
public string Name { get; set; }
public IFormFile File { get; set; }
}
//控制器
namespace PostProjectEvaluations.Web.Controllers
{
public class projectsController : Controller
{
private readonly IApplicationRepository ApplicationRepository;
private readonly PostProjectEvaluationsContext _context;
private IHostingEnvironment mxHostingEnvironment { get; set; }
private object objproject;
public projectsController(IApplicationRepository applicationRepository,
IHostingEnvironment hostingEnvironment, PostProjectEvaluationsContext context)
{
mxHostingEnvironment = hostingEnvironment;
ApplicationRepository = applicationRepository;
_context = context;
}
public IActionResult Index()
{
ViewBag.dataSource = ApplicationRepository.GetAllProjects().ToList();
var projects = ApplicationRepository.GetAllProjects();
return View(projects);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(ProjectsVM projectsVM)
{
if (projectsVM.File != null)
{
//upload files to wwwroot
var fileName = Path.GetFileName(projectsVM.File.FileName);
var filePath = Path.Combine(mxHostingEnvironment.WebRootPath, "Uploads", fileName);
using (var fileSteam = new FileStream(filePath, FileMode.Create))
{
await projectsVM.File.CopyToAsync(fileSteam);
}
//your logic to save filePath to database, for example
Projects projects = new Projects();
projects.Name = projectsVM.Name;
projects.FilePath = filePath;
_context.Projects.Add(projects);
_context.SaveChanges();
}
else
{
}
return View("Index");
}
public IActionResult Details(int id)
{
var project = ApplicationRepository.GetProjects(id);
return View(project);
}
[HttpGet]
public IActionResult Create()
{
var project = new Projects();
return View(project);
}
[HttpPost]
public IActionResult Create(Projects projects)
{
ApplicationRepository.Create(projects);
return RedirectToAction("Index");
}
public IActionResult Delete(int id)
{
var project = ApplicationRepository.GetProjects(id);
ApplicationRepository.Delete(project);
return RedirectToAction("Index");
}
[HttpGet]
public IActionResult Edit(int id)
{
var project = ApplicationRepository.GetProjects(id);
//mxApplicationRepository.SaveChangesAsync();
return View(project);
}
[HttpPost]
public IActionResult Edit(Projects projects)
{
ApplicationRepository.Edit(projects);
//mxApplicationRepository.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
//看法
<form enctype="multipart/form-data" asp-controller="Projects" asp-action="Create" method="post" class="form-horizontal">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-body">
<h3 class="form-section">Project Info</h3>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-3">Project Name</label>
<div class="col-md-9">
<input asp-for="Name" class="form-control" placeholder="Name">
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
</div>
<!--/span-->
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-3">Project Manager</label>
<div class="col-md-9">
<input asp-for="Manager" class="form-control" placeholder="Name">
</div>
</div>
</div>
<!--/span-->
</div>
<!--/span-->
</div>
<h3 class="form-section">Project Files</h3>
<!--/row-->
<div class="row">
<div>
<div class="col-md-6">
<div class="col-md-10">
<p>Upload one or more files using this form:</p>
<input type="file" name="files" multiple />
</div>
</div>
</div>
<div>
</div>
</div>
<div class="form-actions right">
<input type="submit" class="btn blue-assembly" name="submitButton" value="Save" />
<a asp-action="Index" class="btn default" onclick="cancelClick()">Cancel</a>
</div>
解决方案
在第一行中包含您的模型
@ProjectsVM
您必须提供与模型相同的名称试试这个:
<input type="file" id= name="File" multiple />
推荐阅读
- python - 如何按 Python 中的第一个元素重新分类元组列表?
- apache-spark - Apache Spark:获取每个分区的第一行和最后一行
- r - 以前安装的版本中的 R 包在哪里?
- mysql - 如何将我的 React Native 应用程序连接到 MySQL 服务器?
- python - 矩阵中的for循环
- scala - 如何测试返回 Dataset[String, T] 的函数
- javascript - 为什么调用 fetch、axios、ajax 或 promise 时函数返回未定义
- python - Locust 负载测试脚本出错 - 连接中止。',RemoteDisconnected
- primary-key - 雪花批量加载的 AUTOINCREMENT 主键
- java - Quadtrees 应该只在孩子中存储点吗?