【发布时间】:2022-10-14 13:48:02
【问题描述】:
我正在尝试使用 FilePond 将文件上传到我的项目。我想将文件保存在文件中(图像/横幅)。但是当我在输入变量中添加 class="filepond" 时,IFormFile 返回 null。知道如何解决这个问题吗?
@model InputViewModel
<form method="post" asp-controller="Home" asp-action="Create" enctype="multipart/form-data">
<input type="file"
class="filepond"
name="postedFiles"
multiple
data-allow-reorder="true"
data-max-file-size="3MB"
data-max-files="3">
<button class="btn btn-primary" type="submit" style="visibility:visible">Save</button>
</form>
控制器
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IWebHostEnvironment _hostEnvironment;
public HomeController(ILogger<HomeController> logger,IWebHostEnvironment hostEnvironment)
{
_logger = logger;
_hostEnvironment = hostEnvironment;
}
public async Task<IActionResult> Create(InputViewModel command)
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(InputViewModel command, List<IFormFile> postedFiles)
{
string wwwPath = this._hostEnvironment.WebRootPath;
string contentPath = this._hostEnvironment.ContentRootPath;
string path = Path.Combine(this._hostEnvironment.WebRootPath, @"images\banner");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
List<string> uploadedFiles = new List<string>();
foreach (IFormFile postedFile in postedFiles)
{
string fileName = Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
uploadedFiles.Add(fileName);
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
}
return View();
}
【问题讨论】:
-
你能显示有效载荷和响应吗?
标签: c# asp.net-mvc model-view-controller filepond