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