IFormFile拒绝对路径的ASP.Net Core访问 [英] ASP.Net Core Access to the Path is Denied with IFormFile
问题描述
只需编写一个简单的ASP.NET Core WebAPI并使用接受IFormFiles的简单POST端点即可:
Just writing a simple ASP.NET Core WebAPI and when using a simple POST endpoint accepting IFormFiles:
[HttpPost]
public async Task<List<string>> Post(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
List<string> result = new List<string>();
Console.WriteLine(files.Count);
foreach (var f in files)
{
if (f.Length > 0)
{
Directory.CreateDirectory("Resources");
using (var stream = new FileStream("Resources", FileMode.Create))
{
await f.CopyToAsync(stream);
result.Add(f.FileName);
}
}
}
return result;
}
我收到此错误:
System.UnauthorizedAccessException:拒绝访问路径
'F:\Documents HDD\spec-backend\Resources'
System.UnauthorizedAccessException: Access to the path 'F:\Documents HDD\spec-backend\Resources' is denied
我已经研究过了,显然这与我的目录是只读有关,但是我不知道如何更改它,即使这样我仍然在创建目录仍然是ASP.NET控制器。
I have looked into it and apparently it has something to do with my directory being readonly but I cannot figure out how to change this and even then the directory is being created by my ASP.NET controller anyway.
推荐答案
最后的答案是FileStream对象需要路径中文件的名称,而不只是目录。
The answer in the end was that the FileStream object required the name of the file in the path, not just the directory.
using (var stream = new FileStream(Path.Combine("Resources", f.FileName), FileMode.Create))
{
await f.CopyToAsync(stream);
result.Add(f.FileName);
}
这篇关于IFormFile拒绝对路径的ASP.Net Core访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!