How to save IFormFile to disk?

∥☆過路亽.° 提交于 2019-11-30 05:38:48

A few things have changed since core's release candidates

public class ProfileController : Controller {
    private IHostingEnvironment _hostingEnvironment;

    public ProfileController(IHostingEnvironment environment) {
        _hostingEnvironment = environment;
    }

    [HttpPost]
    public async Task<IActionResult> Upload(IList<IFormFile> 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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!