我试图编写一个操作来上传文件,当我试图调用GetFileName()方法时,我得到了以下错误:
'IFormFile' does not contain a definition for 'GetFileName' and no accessible extension method 'GetFileName' accepting a first argument of type 'IFormFile' could be found (are you missing a using directive or an assembly reference?)我的控制器使用以下名称空间:
using Microsoft.Extensions.FileProviders;
using System.IO;
using Microsoft.AspNetCore.Http;行动是:
[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
if (file == null || file.Length == 0)
return Content("file not selected");
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot", file.GetFileName());
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return RedirectToAction("Files");
} 发布于 2021-05-16 07:50:16
要使用IFormFile获取上传文件的文件名,我们可以使用file.FileName获取它。
试试这个:
[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
if (file == null || file.Length == 0)
return Content("file not selected");
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot", file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return RedirectToAction("Files");
} https://stackoverflow.com/questions/67554194
复制相似问题