如何将IFormFile保存到磁盘?
我正在尝试使用这段代码将文件保存在磁盘上。
IHostingEnvironment _hostingEnvironment; public ProfileController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } [HttpPost] public async Task Upload(IList files) { foreach (var file in files) { var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); var filePath = _hostingEnvironment.WebRootPath + "\\wwwroot\\" + fileName; await file.SaveAsAsync(filePath); } return View(); }
我能够用IHostingEnvironment替换IApplicationEnvironment,用WebRootPath替换ApplicationBasePath 。
似乎IFormFile不再具有SaveAsAsync() 。 如何将文件保存到磁盘呢?
自核心发布候选人以来,一些事情发生了变化
public class ProfileController : Controller { private IHostingEnvironment _hostingEnvironment; public ProfileController(IHostingEnvironment environment) { _hostingEnvironment = environment; } [HttpPost] public async Task Upload(IList files) { var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); foreach (var file in files) { if (file.Length > 0) { var filePath = Path.Combine(uploads, file.FileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } } } return View(); } }
- 使用ASP .NET Core Identity和EntityFrameworkCore注册新用户时出现InvalidOperationException
- 路由控制器和中间件中的操作
- 使用XUnit和ASP.NET Core 1.0进行dependency injection
- 覆盖ASP.NET Core MVC 1.0中的全局授权filter
- 没有为“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory”类型提供服务
- 在mvc中使用IViewLocationExpander
- 通过.NET Core控制台应用程序中的dependency injection访问配置
- 引用.NET Core XUnit项目中的标准dll
- ActionContext在Microsoft.AspNetCore.Mvc.Controller中消失了