如何在 ASP.NET 中将文件路径转换为 URL
题
基本上我有一些代码来检查特定目录以查看图像是否存在,如果存在,我想将图像的 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;
}
}
解决方案
据我所知,没有办法做你想做的事;至少不是直接的。我会存储 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);
}
}
其他提示
这就是我用的:
private string MapURL(string path)
{
string appPath = Server.MapPath("/").ToLower();
return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\", "/"));
}
所有这些答案的问题在于它们没有考虑虚拟目录。
考虑:
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"
因为没有办法获得虚拟目录的完整列表。
我已经接受了 Fredriks 的回答,因为它似乎可以用最少的努力解决问题,但是 Request 对象似乎并不包含 ResolveUrl 方法。可以通过 Page 对象或 Image 控件对象访问它:
myImage.ImageUrl = Page.ResolveUrl(photoURL);
myImage.ImageUrl = myImage.ResolveUrl(photoURL);
如果您像我一样使用静态类,另一种选择是使用 VirtualPathUtility:
myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);
也许这不是最好的方法,但它确实有效。
// 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)。图像的物理路径应该包含服务器的路径,因此“减去”它们,只留下图像相对于服务器的路径(例如:\设计\图片.jpg)。将反斜杠替换为斜杠并将其附加到服务器的 url 中。
这对我有用:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";
据我所知,没有一个函数可以做到这一点(也许您正在寻找相反的函数) 地图路径?)。我很想知道是否存在这样的功能。在那之前,我只需获取 GetFiles 返回的文件名,删除路径,并在前面添加 URL 根。这可以一般地完成。
简单的解决方案似乎是在网站内设置一个临时位置,您可以通过 URL 轻松访问该位置,然后在需要保存文件时可以将文件移动到物理位置。
获取 URL 的左侧部分:
?HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
"http://localhost:1714"
要获取应用程序(网络)名称:
?HttpRuntime.AppDomainAppVirtualPath
"/"
这样,您就可以在获取完整 URL 后添加相对路径。
我认为这应该有效。它可能会在斜杠上脱落。不确定是否需要它们。
string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];