[英]What permissions are required to upload to WWWRoot folder in ASP.NET Core API
我已经创建了asp.net core 2.2 Web API + MVC项目,并且试图将文件上传到WWWRoot文件夹。 尝试上传文件时出现访问被拒绝错误。
我想知道适当的帐户和所需的权限以启用写访问权限
我在启动文件中应用了UseStaticFiles。
这是上传文件的代码:
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);
}
使用下面的代码为我工作
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);
}
}
}
另外,尝试以管理员身份运行Visual Studio,以允许完全访问计算机中的文件,文件夹
如Joe的建议,请确保该文件夹存在,然后尝试将fileName追加到您的文件夹路径中,如下所示:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.