2011-06-22 19 views
9

この物理パス"C:\bla\bla\Content\Upload\image.jpg""/Content/Upload/image.jpg"のようなサーバーパスに変換します。物理パスのサーバパスを取得する方法は?

どうすればいいですか?相対パスを取得するには、以下を行うことができます

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); 
+0

ここで、クライアント側またはサーバー側では? – jsalonen

+0

@jsalonen - サーバー側 – Freshblood

+0

[ASP.NET絶対パスからWeb相対パスに戻る]の重複可能性(http://stackoverflow.com/questions/3164/asp-net-absolute-path-back-to-web-相対パス) –

答えて

11

あなたがそのようなものを使用することができます。

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

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

OPとは逆のことではありませんか? – GvS

+0

ああ申し訳ありませんが、私は応答を –

+2

書き直しましたが、その良い解決策ですが、私はHttpServerUtilityの代わりにHttpServerUtilityの代わりにHttpServerUtilityBaseを使用することを好むHttpRequestBaseの代わりにHttpRequestBase。うまくいく。 –

関連する問題