简体   繁体   English

上载到ASP.NET Core API中的WWWRoot文件夹需要什么权限

[英]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.

相关问题 文件上传到ASP.NET Core中的wwwroot文件夹 - File upload to wwwroot folder in ASP.NET Core 将“react js”构建到我的“asp.net core web api”的 wwwroot 文件夹时出现路由问题 - Routing problem when building "react js" to wwwroot folder of my "asp.net core web api" 从 Asp.net 内核 Web API 3.1 “wwwroot”之外的文件夹加载图像 - Loading Image from Asp.net core Web API 3.1 Folder outside of “wwwroot” 更新到 Asp.Net Core 3 的 Identity 项目提供了 wwwroot 文件夹 - Identity project updated to Asp.Net Core 3 gives wwwroot folder 在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式) - Using a wwwroot folder (ASP.NET Core style) in ASP.NET 4.5 project 什么是asp.net vnext中的wwwroot - What is wwwroot in asp.net vnext C#asp.net核心v。2自托管如何将图片上传到wwwroot下的子文件夹? - C# asp.net core v. 2 self hosted how to upload image to subfolder under wwwroot? “wwwroot”文件夹中的某些文件未在 ASP.NET 核心 web 部署中发布 - Some files in "wwwroot" folder are not published in ASP.NET Core web deploy IIS 部署不能在 asp.net core 2.2 razor 页面中包含 wwwroot 文件夹的文件 - IIS deployment cannot contains files for wwwroot folder in asp.net core 2.2 razor pages 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM