Server.MapPath()没有获取特定文件夹

时间:2014-04-07 12:13:07

标签: asp.net web-services server.mappath

我正在尝试将图像文件保存在我的WebApplication文件夹中,但它无法正常工作 这是目录图

wwwroot
  -
  -
  -----RegApp
  -      -
  -      ----Images
  -
  -
  -----RegService

我正在将文件从WebService(RegService)保存到WebApp(RegApp)目录中 这是来自WebService的代码

File.WriteAllBytes(Server.MapPath("../RegApp//Images//" + Email + ".jpeg"), Convert.FromBase64String(PictureByteString));

但它给了我这个

System.Web.HttpException: Cannot use a leading .. to exit above the top directory.
   at System.Web.Util.UrlPath.ReduceVirtualPath(String path)
   at System.Web.Util.UrlPath.Reduce(String path)
   at System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative)
   at System.Web.VirtualPath.Combine(VirtualPath relativePath)
   at System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping)
   at System.Web.HttpRequest.MapPath(VirtualPath virtualPath)
   at System.Web.HttpServerUtility.MapPath(String path)

2 个答案:

答案 0 :(得分:0)

我猜问题是使用" ../"在MapPath中,一种解决方法是调用MapPath("。"),然后使用System.IO类去任何你想去的地方。

string baseDir = Server.MapPath(".");
string imageName = Directory.GetParent(baseDir).FullName + "/RegApp//Images//" + Email + ".jpeg";

File.WriteAllBytes(imageName, Convert.FromBase64String(PictureByteString));

另一种解决方案是将物理目录位置存储在配置密钥中,并完全抛弃MapPath。

在这两种情况下,您都必须有权访问该目录。

答案 1 :(得分:0)

最后通过使用虚拟目录找到了一种简单的方法。现在我使用C:\Images服务器文件夹通过webService存储图像,并将Images文件夹添加到{ {1}}作为虚拟文件夹,WebApp文件夹中存在C:\Images文件夹中的内容...