asp.net core mvc 读取文件

IFormFileIEnumerable<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(); }

  

原文地址:https://www.cnblogs.com/xiyoujiyjy/p/5822107.html