Ric*_*d77 37 c# asp.net-core-1.0
我正在尝试使用这段代码将文件保存在磁盘上.
IHostingEnvironment _hostingEnvironment;
public ProfileController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
[HttpPost]
public async Task<IActionResult> Upload(IList<IFormFile> 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();
}
Run Code Online (Sandbox Code Playgroud)
我能代替IApplicationEnvironment与IHostingEnvironment,并ApplicationBasePath与WebRootPath.
似乎IFormFile不再具有SaveAsAsync()了.如何将文件保存到磁盘呢?
Nko*_*osi 62
自核心发布候选人以来,一些事情发生了变化
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();
}
}
Run Code Online (Sandbox Code Playgroud)