首页 > 解决方案 > 尝试获取服务器端图像进行处理时出现“访问路径被拒绝”错误

问题描述

我正在研究一个解决方案,在该解决方案中,我需要将上传的图像转换为 base 64 字符串,我的代码在 localhost 上运行良好,但是当我将它上传到服务器时,它让我可以访问路径'C:\ Inetpub\vhosts\xyz.com\xyz.com\Resources\Address\1002055.jpg' 被拒绝。

这是我的代码片段,

strFileName = fuPhoto.FileName;
strFileExtension = Path.GetExtension(fuPhoto.FileName);
if (strFileExtension.ToLower() == ".jpg" || strFileExtension.ToLower() == ".jpeg" || strFileExtension.ToLower() == ".png")
{
CSPProfileFile = Functions.GetFileName(hdnCSPCode.Value + strFileExtension);
fuPhoto.SaveAs(Server.MapPath(Path.Combine("~/Resources/Profile/", CSPProfileFile)));
base64_photo = ConvertToBase64Image(Server.MapPath("~/Resources/Profile/"+CSPProfileFile));
}

我使用了一个简单的base64字符串转换方法

internal static string ConvertToBase64Image(string file)
{ 
return Convert.ToBase64String(File.ReadAllBytes(file));
}

此代码在 localhost 环境中运行良好,但当我将其发布到服务器时出现错误 - 拒绝访问路径'C:\Inetpub\vhosts\xyz.com\xyz.com\Resources\Address\1002055.jpg'。

以前相同的代码在服务器上也可以正常工作,我是不是写错了..

标签: c#asp.netfile-upload

解决方案


推荐阅读