尝试使用 asp.net web api 2 将图像上传到服务器时访问被拒绝
access is denied when trying to upload image to the server using asp.net web api 2
我正在尝试使用 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";
}
当我通过邮递员上传图片时,我得到
"Access to the path 'c:\users\ahmed\source\repos\Election\Election\Uploads\Background.png' is denied."
检查您的上传文件夹权限。并添加每个人上传文件夹的权限,并再次测试。测试结束后不要忘记取消所有人权限。
我正在尝试使用 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";
}
当我通过邮递员上传图片时,我得到
"Access to the path 'c:\users\ahmed\source\repos\Election\Election\Uploads\Background.png' is denied."
检查您的上传文件夹权限。并添加每个人上传文件夹的权限,并再次测试。测试结束后不要忘记取消所有人权限。