多文件上传只存储数据库中的最后一个文件 Asp .Net Core MVC
Multiple File upload storing only last file in database Asp .Net Core MVC
我正在尝试上传多个文件。文件已正确上传,但在数据库中,仅存储最后一个文件
这是我的 post 方法
代码
public async Task<IActionResult> ApplyLeave(ApplyLeaveViewModel model)
{
string uniqueFileName = null;
if (model.File != null && model.File.Count > 0)
{
foreach (IFormFile file in model.File)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "dist/files");
uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
file.CopyTo(new FileStream(filePath, FileMode.Create));
}
var leaveApplied = _mapper.Map<LeaveViewModel>(model);
leaveApplied.FilePath = uniqueFileName;
await _leavePageService.AddNewLeave(leaveApplied);
}
return View(model);
} }
这部分ApplyLeaveViewModel对应离开属性
public string Reason { get; set; }
public string Comment { get; set; }
public List<IFormFile> File { get; set; }
这是正在映射并将数据存储在数据库中的 LeaveViewModel
public string FilePath { get; set; }
我知道 uniqueFileName 是一个字符串,一次只存储一个值,所以它显示最后一个值。怎么办,让上传的文件全部存入数据库
I understand that uniqueFileName is a string which is storing only one value at a time, so it displays the last value.
你在foreach (IFormFile file in model.File)
结束后得到uniqueFileName,所以它的值是最后一个文件的名称。
第一个解决方案是你不改变你的模型,即使用public string FilePath {get; set;}
存储多个文件的唯一文件名。
- 您需要连接所有的uniqueFileNames,并用字符(例如|)分隔uniqueFileNames。
-
string allfilesPath = null;
if (model.File != null && model.File.Count > 0)
{
foreach (IFormFile file in model.File)
{
... ...
allfilesPath += "|" + uniqueFileName;
}
var leaveApplied = _mapper.Map<LeaveModel>(model);
leaveApplied.AllFiles = allfilesPath;
- 下次取值时,需要使用String.Split方法对字符串进行拆分得到每一个唯一文件名。
第二种解决方案是:你可以添加一个名为FileModel[=的新模型 67=],用来存放文件信息,可以设置导航属性让LeaveModel和FileModel有一个一对多关系.
我写了一个例子,你可以参考一下
型号
public class LeaveModel
{
[Key]
public int LeaveId { get; set; }
public string Reason { get; set; }
public string Comment { get; set; }
public List<FileModel> AllFiles { get; set; }
}
public class FileModel
{
[Key]
public int FileId { get; set; }
public string FilePath { get; set; }
public int? LeaveId { get; set; }
}
public class ApplyLeaveViewModel
{
public string Reason { get; set; }
public string Comment { get; set; }
public List<IFormFile> File { get; set; }
}
离开Controller
public class LeaveController : Controller
{
public IWebHostEnvironment _webHostEnvironment;
private readonly IMapper _mapper;
public DailyCoreMVCDemoContext _db;
public LeaveController(IWebHostEnvironment webHostEnvironment, IMapper mapper, DailyCoreMVCDemoContext db)
{
_mapper = mapper;
_webHostEnvironment = webHostEnvironment;
_db = db;
}
public IActionResult Index()
{
return View();
}
public IActionResult ApplyLeave(ApplyLeaveViewModel model)
{
string uniqueFileName = null;
if (model.File != null && model.File.Count > 0)
{
List<FileModel> fileModels = new List<FileModel>();
foreach (IFormFile file in model.File)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "dist/files");
if (!Directory.Exists(uploadsFolder))
{
Directory.CreateDirectory(uploadsFolder);
}
uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
file.CopyTo(new FileStream(filePath, FileMode.Create));
fileModels.Add(new FileModel { FilePath = uniqueFileName });
}
var leaveApplied = _mapper.Map<LeaveModel>(model);
leaveApplied.AllFiles = fileModels;
_db.LeaveModels.Add(leaveApplied);
_db.SaveChanges();
}
return RedirectToAction("Index");
}
}
索引
@model WebApplication24.Models.ApplyLeaveViewModel
<form asp-controller="Leave" asp-action="ApplyLeave" method="post" enctype="multipart/form-data">
<input asp-for="Comment"/>
<input asp-for="Reason"/>
<input asp-for="File" type="file" multiple/>
<button type="submit">submit</button>
</form>
结果
我正在尝试上传多个文件。文件已正确上传,但在数据库中,仅存储最后一个文件 这是我的 post 方法
代码 public async Task<IActionResult> ApplyLeave(ApplyLeaveViewModel model)
{
string uniqueFileName = null;
if (model.File != null && model.File.Count > 0)
{
foreach (IFormFile file in model.File)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "dist/files");
uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
file.CopyTo(new FileStream(filePath, FileMode.Create));
}
var leaveApplied = _mapper.Map<LeaveViewModel>(model);
leaveApplied.FilePath = uniqueFileName;
await _leavePageService.AddNewLeave(leaveApplied);
}
return View(model);
} }
这部分ApplyLeaveViewModel对应离开属性
public string Reason { get; set; }
public string Comment { get; set; }
public List<IFormFile> File { get; set; }
这是正在映射并将数据存储在数据库中的 LeaveViewModel
public string FilePath { get; set; }
我知道 uniqueFileName 是一个字符串,一次只存储一个值,所以它显示最后一个值。怎么办,让上传的文件全部存入数据库
I understand that uniqueFileName is a string which is storing only one value at a time, so it displays the last value.
你在foreach (IFormFile file in model.File)
结束后得到uniqueFileName,所以它的值是最后一个文件的名称。
第一个解决方案是你不改变你的模型,即使用
public string FilePath {get; set;}
存储多个文件的唯一文件名。- 您需要连接所有的uniqueFileNames,并用字符(例如|)分隔uniqueFileNames。
-
string allfilesPath = null; if (model.File != null && model.File.Count > 0) { foreach (IFormFile file in model.File) { ... ... allfilesPath += "|" + uniqueFileName; } var leaveApplied = _mapper.Map<LeaveModel>(model); leaveApplied.AllFiles = allfilesPath;
-
- 下次取值时,需要使用String.Split方法对字符串进行拆分得到每一个唯一文件名。
- 您需要连接所有的uniqueFileNames,并用字符(例如|)分隔uniqueFileNames。
第二种解决方案是:你可以添加一个名为FileModel[=的新模型 67=],用来存放文件信息,可以设置导航属性让LeaveModel和FileModel有一个一对多关系.
我写了一个例子,你可以参考一下
型号
public class LeaveModel { [Key] public int LeaveId { get; set; } public string Reason { get; set; } public string Comment { get; set; } public List<FileModel> AllFiles { get; set; } } public class FileModel { [Key] public int FileId { get; set; } public string FilePath { get; set; } public int? LeaveId { get; set; } } public class ApplyLeaveViewModel { public string Reason { get; set; } public string Comment { get; set; } public List<IFormFile> File { get; set; } }
离开Controller
public class LeaveController : Controller { public IWebHostEnvironment _webHostEnvironment; private readonly IMapper _mapper; public DailyCoreMVCDemoContext _db; public LeaveController(IWebHostEnvironment webHostEnvironment, IMapper mapper, DailyCoreMVCDemoContext db) { _mapper = mapper; _webHostEnvironment = webHostEnvironment; _db = db; } public IActionResult Index() { return View(); } public IActionResult ApplyLeave(ApplyLeaveViewModel model) { string uniqueFileName = null; if (model.File != null && model.File.Count > 0) { List<FileModel> fileModels = new List<FileModel>(); foreach (IFormFile file in model.File) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "dist/files"); if (!Directory.Exists(uploadsFolder)) { Directory.CreateDirectory(uploadsFolder); } uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); file.CopyTo(new FileStream(filePath, FileMode.Create)); fileModels.Add(new FileModel { FilePath = uniqueFileName }); } var leaveApplied = _mapper.Map<LeaveModel>(model); leaveApplied.AllFiles = fileModels; _db.LeaveModels.Add(leaveApplied); _db.SaveChanges(); } return RedirectToAction("Index"); } }
索引
@model WebApplication24.Models.ApplyLeaveViewModel <form asp-controller="Leave" asp-action="ApplyLeave" method="post" enctype="multipart/form-data"> <input asp-for="Comment"/> <input asp-for="Reason"/> <input asp-for="File" type="file" multiple/> <button type="submit">submit</button> </form>
结果