如何将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(); } }
- 通过.NET Core控制台应用程序中的dependency injection访问配置
- 如何使用ASP.NET Core进行流式处理
- 如何在ASP.NET Core 1.0的DI中的Startup类中添加IHttpContextAccessor?
- asp.net mvc core以下方法或属性之间的调用不明确:
- ASP.NET CORE 1.0,模拟
- ASP.NET Core RC2种子数据库
- 使用ASP .NET Core Identity和EntityFrameworkCore注册新用户时出现InvalidOperationException
- 在ASP.NET核心中注入IUrlHelper
- 在.NET Core中使用DataTable