如何将 IFormFile 保存到磁盘?
How to save IFormFile to disk?
我正在尝试使用 this piece of code 在磁盘上保存文件。
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();
}
我能够将 IApplicationEnvironment 替换为 IHostingEnvironment,将 ApplicationBasePath 替换为 WebRootPath.
IFormFile 似乎不再有 SaveAsAsync()。那么如何将文件保存到磁盘?
自 core 发布候选版本以来,发生了一些变化
public class ProfileController : Controller {
private IWebHostEnvironment _hostingEnvironment;
public ProfileController(IWebHostEnvironment environment) {
_hostingEnvironment = environment;
}
[HttpPost]
public async Task<IActionResult> Upload(IList<IFormFile> files) {
string uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
foreach (IFormFile file in files) {
if (file.Length > 0) {
string filePath = Path.Combine(uploads, file.FileName);
using (Stream fileStream = new FileStream(filePath, FileMode.Create)) {
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
}
Core 3.0 中会有进一步的变化,因为 IHostingEnvironment 现在被标记为已过时。
using Microsoft.Extensions.Hosting;
public class ProfileController : Controller
{
private IHostEnvironment _hostingEnvironment;
public ProfileController(IHostEnvironment environment)
{
_hostingEnvironment = environment;
}
我正在尝试使用 this piece of code 在磁盘上保存文件。
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();
}
我能够将 IApplicationEnvironment 替换为 IHostingEnvironment,将 ApplicationBasePath 替换为 WebRootPath.
IFormFile 似乎不再有 SaveAsAsync()。那么如何将文件保存到磁盘?
自 core 发布候选版本以来,发生了一些变化
public class ProfileController : Controller {
private IWebHostEnvironment _hostingEnvironment;
public ProfileController(IWebHostEnvironment environment) {
_hostingEnvironment = environment;
}
[HttpPost]
public async Task<IActionResult> Upload(IList<IFormFile> files) {
string uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
foreach (IFormFile file in files) {
if (file.Length > 0) {
string filePath = Path.Combine(uploads, file.FileName);
using (Stream fileStream = new FileStream(filePath, FileMode.Create)) {
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
}
Core 3.0 中会有进一步的变化,因为 IHostingEnvironment 现在被标记为已过时。
using Microsoft.Extensions.Hosting;
public class ProfileController : Controller
{
private IHostEnvironment _hostingEnvironment;
public ProfileController(IHostEnvironment environment)
{
_hostingEnvironment = environment;
}