简体   繁体   English

尝试使用asp.net Web API 2将图像上传到服务器时,访问被拒绝

[英]access is denied when trying to upload image to the server using asp.net web api 2

I am trying to upload image to the server with WebAPI C# Code: 我正在尝试使用WebAPI C#代码将图像上传到服务器:

 public async Task<string> Post()
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                foreach(string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    var filename = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

                    var filePath = HttpContext.Current.Server.MapPath("~/Uploads/"+ filename);

                    postedFile.SaveAs(filePath);

                    return "/uploads" + filename;
                }
            }
            else
            {
                return "file was not uploaded";
            }
        }

        catch (Exception exception)
        {
            return exception.Message;
        }
        return "hi";
    }

When I upload image through postman, I get 通过邮递员上传图片时,我得到

"Access to the path 'c:\\users\\ahmed\\source\\repos\\Election\\Election\\Uploads\\Background.png' is denied." “拒绝访问路径'c:\\ users \\ ahmed \\ source \\ repos \\ Election \\ Election \\ Uploads \\ Background.png'。”

Check your upload folder permissions. 检查您的上传文件夹权限。 And add everyone permission to upload folder, and test it again. 并向所有人添加上传文件夹的权限,然后再次进行测试。 Don't forget remove everyone permission when test is over. 测试结束后,请不要忘记删除所有人的权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用asp.net Web应用程序提取系统信息时拒绝访问 - Getting access denied when trying to extract system information using a asp.net web application ASP.NET Web API将图像上传到SQL Server数据库 - ASP.NET Web API upload image to SQL Server database ASP.NET Web API上传图片 - ASP.NET Web API upload image ASP.Net“访问路径&#39;&#39;被拒绝。”文件上传控制在Web服务器(IIS7)中不起作用 - ASP.Net “Access to the path '' is denied.”File Upload Control not Working in Web Server (IIS7) ASP.net 收到错误“访问路径被拒绝”。 在尝试将文件上传到我的 Windows Server 2008 R2 Web 服务器时 - ASP.net Getting the error “Access to the path is denied.” while trying to upload files to my Windows Server 2008 R2 Web server HTTP 403 Forbidden: Access is denied ASP.NET Web API - HTTP 403 Forbidden: Access is denied ASP.NET Web API ASP.NET访问Web服务器上拒绝的路径 - ASP.NET Access to the path denied, on Web Server ASP.NET WEB API无法将多部分/表单数据(图像)上传到Web服务器 - ASP.NET WEB API cant upload multipart/form-data (image) to Web server 尝试使用Ajax将png上载到Asp.net Core服务器时出现“错误的Content-Type:image / png” - “Incorrect Content-Type: image/png” when trying to upload png with Ajax to Asp.net Core server 将文件上传到 ASP.NET Web API 时出现 Access-Control-Allow-Origin HTTP 500 错误 - Access-Control-Allow-Origin HTTP 500 error when Upload file to ASP.NET Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM