C# 上载到ASP.NET核心API中的WWWRoot文件夹需要哪些权限
我已经创建了asp.net core 2.2 web API+MVC项目,我正在尝试将一个文件上载到WWWRoot文件夹。尝试上载文件时出现访问被拒绝错误 我想知道启用写访问权限所需的适当帐户和权限 我已经在启动文件中应用了UseStaticFiles 以下是上传文件的代码:C# 上载到ASP.NET核心API中的WWWRoot文件夹需要哪些权限,c#,asp.net-core,C#,Asp.net Core,我已经创建了asp.net core 2.2 web API+MVC项目,我正在尝试将一个文件上载到WWWRoot文件夹。尝试上载文件时出现访问被拒绝错误 我想知道启用写访问权限所需的适当帐户和权限 我已经在启动文件中应用了UseStaticFiles 以下是上传文件的代码: public async Task<IActionResult> Create(FileuploadRequest fileuploadRequest) { if (
public async Task<IActionResult> Create(FileuploadRequest fileuploadRequest)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
else
{
if (fileuploadRequest.Logo.Length > 0)
{
string webRootPath = _hostingEnvironment.WebRootPath;
var folderPath = Path.Combine(webRootPath,"Resources\\Images");
using (var fileStream = new FileStream(folderPath, FileMode.Create))
{
await fileuploadRequest.Logo.CopyToAsync(fileStream);
}
公共异步任务创建(FileUploadRequestFileUploadRequest)
{
如果(!ModelState.IsValid)
{
返回请求();
}
其他的
{
如果(fileuploadRequest.Logo.Length>0)
{
字符串webRootPath=\u hostingEnvironment.webRootPath;
var folderPath=Path.Combine(webRootPath,“资源\\图像”);
使用(var fileStream=newfilestream(folderPath,FileMode.Create))
{
等待fileuploadRequest.Logo.CopyToAsync(fileStream);
}
使用下面的代码对我有用
public async Task<string> UploadAssets(IFormFile file, string fileName)
{
string storePath = "";
try
{
storePath = Path.Combine(_assetSettings.Value.StorePath, $"{fileName}.{file.ContentType.Split("/")[1]}");
using (var stream = new FileStream(storePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
}
公共异步任务上载资产(IFormFile文件,字符串文件名)
{
字符串storePath=“”;
尝试
{
storePath=Path.Combine(_assetSettings.Value.storePath,$“{fileName}.{file.ContentType.Split(“/”[1]}”);
使用(var stream=newfilestream(storePath,FileMode.Create))
{
等待文件.CopyToAsync(流);
}
}
}
另外,请尝试以管理员权限运行Visual studio,以允许完全访问计算机中的文件和文件夹,请使用下面的代码为我工作
public async Task<string> UploadAssets(IFormFile file, string fileName)
{
string storePath = "";
try
{
storePath = Path.Combine(_assetSettings.Value.StorePath, $"{fileName}.{file.ContentType.Split("/")[1]}");
using (var stream = new FileStream(storePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
}
公共异步任务上载资产(IFormFile文件,字符串文件名)
{
字符串storePath=“”;
尝试
{
storePath=Path.Combine(_assetSettings.Value.storePath,$“{fileName}.{file.ContentType.Split(“/”[1]}”);
使用(var stream=newfilestream(storePath,FileMode.Create))
{
等待文件.CopyToAsync(流);
}
}
}
另外,请尝试以管理员权限运行Visual studio,以允许完全访问计算机中的文件、文件夹,如Joe所建议的,确保文件夹存在,并尝试将文件名附加到文件夹路径,如下所示:
if (fileuploadRequest.Logo.Length > 0)
{
string webRootPath = _hostingEnvironment.WebRootPath;
var fileName = Path.GetFileName(fileuploadRequest.Logo.FileName);
var folderPath = Path.Combine(webRootPath,"Resources\\Images",fileName);
using (var fileStream = new FileStream(folderPath, FileMode.Create))
{
await fileuploadRequest.Logo.CopyToAsync(fileStream);
}
按照Joe的建议,确保文件夹存在,并尝试将文件名附加到文件夹路径,如下所示:
if (fileuploadRequest.Logo.Length > 0)
{
string webRootPath = _hostingEnvironment.WebRootPath;
var fileName = Path.GetFileName(fileuploadRequest.Logo.FileName);
var folderPath = Path.Combine(webRootPath,"Resources\\Images",fileName);
using (var fileStream = new FileStream(folderPath, FileMode.Create))
{
await fileuploadRequest.Logo.CopyToAsync(fileStream);
}
面对类似问题,我无法在调试模式下上载文件。同样,文件夹是否存在wwwroot/Resources/Images?如果不是,这就是导致错误的原因。您还需要将文件名附加到文件夹路径,即它必须是文件的路径,而不是文件夹。面对类似问题,我无法在调试模式下上载文件。同样,我无法在调试模式下上载文件夹是否存在wwwroot/Resources/Images?如果不是,这就是错误的原因,您还需要将文件名附加到文件夹路径,即它需要是文件的路径,而不是文件夹感谢您的解释,文件名没有附加-愚蠢的问题解答,文件名没有附加-愚蠢的问题