asp.net core mvc 读取文件

IFormFile, IEnumerable<IFormFile>

private IHostingEnvironment _environment;

public TestController(IOptions<MyOptions> optionsAccessor, IHostingEnvironment env)

{

_optionsAccessor = optionsAccessor;

_environment = env;

}


[HttpPost] public async Task<IActionResult> Upload(IFormFile file, bool notUsed = false) { if (file != null) { var uploads = Path.Combine(_environment.WebRootPath, "uploads"); using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } } return View(); } [HttpPost] public async Task<ActionResult> Upload() { string path = Path.Combine(_environment.WebRootPath, "uploads"); IFormFile file = Request.Form.Files["file"]; if (file != null) { var uploads = Path.Combine(_environment.WebRootPath, "uploads"); using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } } return View(); }