9

我想将此物理路径转换"C:\bla\bla\Content\Upload\image.jpg"为服务器路径,例如"/Content/Upload/image.jpg".

我怎样才能做到这一点 ?

4

2 回答 2

11

你可以使用类似的东西:

 public static class Extensions        {
        public static string RelativePath(this HttpServerUtility utility, string path, HttpRequest context)
        {
            return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "/").Replace(@"\", "/");
        }
    }

你打电话给

Server.RelativePath(path, Request); 
于 2011-06-22T07:45:20.683 回答
2

您可以执行以下操作来获取相对路径。

String filePath = @"C:\bla\bla\Content\Upload\image.jpg";

String serverPath = Request.PhysicalPath;
String relativePath = filePath.Substring(serverPath.Length, filePath.Length - serverPath.Length);
于 2011-06-22T08:01:42.633 回答