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

如何在ASP.NET中将文件路径转换为URL

  •  45
  • Andy Rose  · 技术社区  · 16 年前

    基本上,我有一些代码来检查一个特定的目录,看看是否有一个图像,如果有,我想给图像分配一个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 回复  |  直到 7 年前
        1
  •  13
  •   Tas    9 年前

    据我所知,没有办法做你想做的事,至少不能直接做。我会储存 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);
        }
    }
    
        2
  •  14
  •   Rafael Herscovici    12 年前

    这是我使用的:

    private string MapURL(string path)
    {
        string appPath = Server.MapPath("/").ToLower();
        return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\", "/"));
     }
    
        3
  •  11
  •   Ross Presser    11 年前

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

    考虑:

    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    16 年前

    我接受了Fredriks的回答,因为它似乎用最少的努力来解决问题,但是请求对象似乎不符合ResolveURL方法。 可以通过页面对象或图像控制对象访问:

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

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

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

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

    // 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)和页面名称(如/whatever.aspx)。图像的物理路径应该包含服务器的路径,因此“substract”它们,只保留图像相对于服务器的路径(如:\design\picture.jpg)。将反斜杠替换为斜杠,并将其附加到服务器的URL。

        6
  •  2
  •   musefan    13 年前

    这对我很有用:

    HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";
    
        7
  •  0
  •   user2189331    16 年前

    据我所知,没有一个函数能做到这一点(也许你在寻找 MapPath ?).我想知道这种功能是否存在。在那之前,我只需要获取getfiles返回的文件名,删除路径,并在url根目录前加上前缀。这可以一般地完成。

        8
  •  0
  •   Brian Herbert    13 年前

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

        9
  •  0
  •   adripanico    12 年前

    获取URL的左侧部分:

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

    获取应用程序(Web)名称:

    ?HttpRuntime.AppDomainAppVirtualPath
    "/"
    

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

        10
  •  -2
  •   Rafael Herscovici    11 年前

    我认为这应该管用。它可能在斜线处断了。不确定是否需要。

    string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];