[英]What permissions are required to upload to WWWRoot folder in ASP.NET Core API
I have created asp.net core 2.2 web API + MVC project and I am trying to upload a file to WWWRoot folder. 我已经创建了asp.net core 2.2 Web API + MVC项目,并且试图将文件上传到WWWRoot文件夹。 I get access denied error when trying to upload file. 尝试上传文件时出现访问被拒绝错误。
I would like to know appropriate accounts and required permissions to enable write access 我想知道适当的帐户和所需的权限以启用写访问权限
I have applied UseStaticFiles in Startup file. 我在启动文件中应用了UseStaticFiles。
Here's code to upload file: 这是上传文件的代码:
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);
}
Using below of code work for me 使用下面的代码为我工作
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);
}
}
}
Also try to run your Visual studio in Administrator right to allow full access to file, folder in your computer 另外,尝试以管理员身份运行Visual Studio,以允许完全访问计算机中的文件,文件夹
As Joe suggested ,make sure that the folder exists and try to append the fileName to your folder path like below: 如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.