【发布时间】:2023-03-29 03:00:01
【问题描述】:
这是我上传文件时的控制器代码。出于某种原因,我不断收到此错误:
DirectoryNotFoundException: 找不到路径的一部分
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> Save(UploadDocumentViewModel Input)
{
var filePath = $"{this.hostingEnvironment.WebRootPath}/documents";
foreach(var item in Request.Form.Files)
{
var fileName = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName;
fileName = fileName.Trim('"');
var fullFilePath = Path.Combine(filePath, fileName);
using(var stream = new FileStream(fullFilePath, FileMode.Create))
{
await item.CopyToAsync(stream);
}
}
return this.Ok();
}
这是我要上传到的目录:
'C:\Users\uuu\Source\Repos\Applicat\Applicto\wwwroot\documents\2018-10-27.png'。
据我了解,它的意思是找不到路径?但是我手动创建了documents文件夹,还是会出现这个错误。
编辑:
控制器顶部:
private readonly ILogger<DocumentsController> logger;
private readonly IHostingEnvironment hostingEnvironment;
public DocumentsController(ILogger<DocumentsController> logger, IHostingEnvironment hostingEnvironment)
{
this.logger = logger;
this.hostingEnvironment = hostingEnvironment;
}
【问题讨论】:
-
能否提供包含
Save方法的Controller或服务构造函数? -
你在哪里创建的目录?
-
@TânNguyễn 我已经提供了构造函数
-
@Shyju 我在 wwwroot 文件夹中创建的
标签: c# asp.net asp.net-core