2011-11-17 106 views
14

我有这个函数获取fileData作为字节数组和文件路径。我得到的错误是当它试图在代码bewlo中设置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)); 
+0

我可以知道为什么投了反对票我是新来的C#。请体谅 – user710502

+0

您了解'MapPath'的作用以及您的代码正在尝试做什么? – SLaks

+0

这是现有的代码,我没有写它..我的理解是,MapPath是一个函数,将采取虚拟路径并将其转换到服务器路径?,但我又是新的,我可能忽略了显而易见的。没有粗鲁,我来到这个论坛问一个我不确定的问题。没有必要投票..我想这是什么论坛是没有?其他方面,我只是想通过个人资料进行个人资料分析,并且仅仅因为它的干扰而做了一堆否定的投票。 – user710502

回答

19

如果你已经有一个物理路径,它没有意义调用Server.MapPath

您打给MapPath两次。

+1

谢谢,所以不是使用Server.MapPath,我应该替换那行代码? – user710502

1

我认为你的项目位于:

E:\WEBS\\webapp\ 

你应该尝试使用相对引用您的图像例如

..\default\images\mains\myimage.jpg 
2

工作:

string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));   
    foreach (string path in filesPath) 
    { 
     FileInfo fi = new FileInfo(path);  //This Is Working 
     string LastAcceTime = fi.LastWriteTime; //Return Correct Value 
    } 

不工作:

string[] filesPath = Directory.GetFiles(Server.MapPath("~/txtPath/"));   
    foreach (string path in filesPath) 
    { 
     FileInfo fi = new FileInfo(Server.MapPath(path)); //This Is Worng 
     string LastAcceTime = fi.LastWriteTime;    //Return 1/1/1601 
    } 

不使用Server.Mappath两次