我有一个函数,它接收文件数据的字节数组和文件路径。我遇到的错误是在下面的代码中尝试设置fileInfo时出现的。它说:“给定物理路径,期望虚拟路径”。
public override void WriteBinaryStorage(byte[] fileData, string filePath)
{
try
{
// Create directory if not exists.
System.IO.FileInfo fileInfo = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)); //when it gets to this line the error is caught
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
// Write the binary content.
System.IO.File.WriteAllBytes(System.Web.HttpContext.Current.Server.MapPath(filePath), fileData);
}
catch (Exception)
{
throw;
}
}
在调试时,将文件路径提供为"E:\\WEBS\\webapp\\default\\images\\mains\\myimage.jpg"
。而错误信息是
'E:/WEBS/webapp/default/images/mains/myimage.jpg' is a physical path, but a virtual path was expected.
同时,引起这种情况发生的是以下调用:
properties.ResizeImage(imageName, Configurations.ConfigSettings.MaxImageSize, Server.MapPath(Configurations.EnvironmentConfig.LargeImagePath));
MapPath
的作用以及你的代码尝试做什么了吗? - SLaks