如何获取物理路径的服务器路径?

时间:2022-05-10 13:25:40

I want to convert this physical path "C:\bla\bla\Content\Upload\image.jpg" to server path like "/Content/Upload/image.jpg".

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

How can i do that ?

我怎样才能做到这一点 ?

2 个解决方案

#1


11  

you can use something like that :

你可以使用这样的东西:

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

and you call

你打电话

Server.RelativePath(path, Request); 

#2


2  

You can do the following to get the relative path.

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

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

String serverPath = Request.PhysicalPath;
String relativePath = filePath.Substring(serverPath.Length, filePath.Length - serverPath.Length);

#1


11  

you can use something like that :

你可以使用这样的东西:

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

and you call

你打电话

Server.RelativePath(path, Request); 

#2


2  

You can do the following to get the relative path.

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

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

String serverPath = Request.PhysicalPath;
String relativePath = filePath.Substring(serverPath.Length, filePath.Length - serverPath.Length);