代码之家  ›  专栏  ›  技术社区  ›  Andy Rose

如何在ASP.NET中将文件路径转换为URL。导航战鉴定小组

  •  47
  • Andy Rose  · 技术社区  · 18 年前

    基本上,我有一些代码来检查特定目录,看看是否有图像,如果有,我想将图像的URL分配给ImageControl。

    if (System.IO.Directory.Exists(photosLocation))
    {
        string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg");
        if (files.Length > 0)
        {
            // TODO: return the url of the first file found;
        }
    }
    
    10 回复  |  直到 11 年前
        1
  •  13
  •   Tas    10 年前

    这是我使用的:

    private string MapURL(string path)
    {
        string appPath = Server.MapPath("/").ToLower();
        return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\", "/"));
     }
    
        2
  •  14
  •   Rafael Herscovici    13 年前

    据我所知,没有办法做你想做的事;至少不是直接的。我会储存 photosLocation 作为相对于应用程序的路径;例如: "~/Images/" 。这样,您可以使用MapPath获取物理位置,以及 ResolveUrl 获取URL(在以下网站的帮助下 System.IO.Path ):

    string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation);
    if (Directory.Exists(photosLocationPath))
    {
        string[] files = Directory.GetFiles(photosLocationPath, "*.jpg");
        if (files.Length > 0)
        {
            string filenameRelative = photosLocation +  Path.GetFilename(files[0])   
            return Page.ResolveUrl(filenameRelative);
        }
    }
    
        3
  •  11
  •   Ross Presser    13 年前

    所有这些答案的问题在于,它们没有考虑到虚拟目录。

    考虑:

    Site named "tempuri.com/" rooted at c:\domains\site
    virtual directory "~/files" at c:\data\files
    virtual directory "~/files/vip" at c:\data\VIPcust\files
    

    所以:

    Server.MapPath("~/files/vip/readme.txt") 
      = "c:\data\VIPcust\files\readme.txt"
    

    但是有 不 方法:

    MagicResolve("c:\data\VIPcust\files\readme.txt") 
       = "http://tempuri.com/files/vip/readme.txt"
    

    因为无法获得虚拟目录的完整列表。

        4
  •  8
  •   Andy Rose    18 年前

    我已经接受了Fredriks的答案,因为它似乎以最少的努力解决了问题,但是Request对象似乎没有连接到ResolveUrl方法中。 这可以通过Page对象或Image控件对象访问:

    myImage.ImageUrl = Page.ResolveUrl(photoURL);
    myImage.ImageUrl = myImage.ResolveUrl(photoURL);
    

    如果你像我一样使用静态类,另一种选择是使用VirtualPathUtility:

    myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);
    
        5
  •  3
  •   Biri    18 年前

    这对我奏效了:

    HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";
    
        6
  •  2
  •   musefan    14 年前

    也许这不是最好的方法,但它奏效了。

    // Here is your path
    String p = photosLocation + "whatever.jpg";
    
    // Here is the page address
    String pa = Page.Request.Url.AbsoluteUri;
    
    // Take the page name    
    String pn = Page.Request.Url.LocalPath;
    
    // Here is the server address    
    String sa = pa.Replace(pn, "");
    
    // Take the physical location of the page    
    String pl = Page.Request.PhysicalPath;
    
    // Replace the backslash with slash in your path    
    pl = pl.Replace("\\", "/");    
    p = p.Replace("\\", "/");
    
    // Root path     
    String rp = pl.Replace(pn, "");
    
    // Take out same path    
    String final = p.Replace(rp, "");
    
    // So your picture's address is    
    String path = sa + final;
    

    编辑:好的,有人标记为没有帮助。一些解释:取当前页面的物理路径,将其分为两部分:服务器和目录(如c:\inetpub\whatever.com\whatever)和页面名称(如/Wevery.aspx)。图像的物理路径应包含服务器的路径,因此“减去”它们,只留下图像相对于服务器的路径(如:\design\picture.jpg)。将反斜杠替换为斜线,并将其附加到服务器的网址上。

        7
  •  0
  •   user2189331    18 年前

    据我所知,没有一个函数可以做到这一点(也许你在寻找逆函数 MapPath ?).我很想知道是否存在这样的功能。在那之前,我只会使用GetFiles返回的文件名,删除路径,并在URL根前添加。这可以一般地完成。

        8
  •  0
  •   Brian Herbert    15 年前

    简单的解决方案似乎是在网站中有一个临时位置,您可以通过URL轻松访问,然后在需要保存文件时可以将文件移动到物理位置。

        9
  •  0
  •   adripanico    13 年前

    获取URL的左侧部分:

    ?HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
    "http://localhost:1714"
    

    要获取应用程序(web)名称:

    ?HttpRuntime.AppDomainAppVirtualPath
    "/"
    

    这样,您就可以在获得完整URL后添加相对路径。

        10
  •  -2
  •   Rafael Herscovici    13 年前

    我认为这应该行得通。它可能会被砍掉。不确定是否需要它们。

    string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];
    
    推荐文章