문제

기본적으로 나는 일부를 확인하는 코드는 특정 디렉토리를 참조하는 경우 이미지가있는 경우 그래서 지정한 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"

하지만 거기에 no 방법:

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

방법이 없기 때문에 전체 목록을 얻을 수있는 가상 디렉토리입니다.

나는 받아들 Fredriks 대답으로 나타나는 문제를 해결하기 위하여 최소한의 노력 그러나 요청을 개체가 나타나지 않을 conatin 의 ResolveUrl 방법입니다.이를 통해 액세스할 수 있는지 개체하거나 이미지를 제어 개체:

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

대체 방법을 사용하는 경우 정적으로 클래스 I,를 사용하는 것입 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 다)및 페이지 이름(아/어떤 것이다.aspx).이미지의 실제 경로 포함해야 하는 서버의 경로,그래서"빼기"그들만 남겨두고,이미지의 상대적인 경로 서버(예:\design\picture.jpg).대체 백슬래쉬로 슬래쉬에 추가하는 서버의 url 이 있습니다.

이것은 나를 위해 일했:

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";

지금까지 내가 아는 없는 기능이다(어쩌면 당신이 찾고있는 역의 MapPath?).나는 사랑을 알고 있는 경우 이러한 함수 존재합니다.그 때까지,저는 그냥 이름(s)반환에 의해 GetFiles,제거 경로와 앞에 추가한 URL 루트입니다.이 할 수 있는 일반적으로.

간단한 솔루션이 될 것으로 보인을 임시 위치를 사이트 내에는 당신이 쉽게 액세스할 수 있으로 URL 을 파일을 이동할 수 있는 물리적 위치 때 당신이 그들을 저장해야합니다.

를 얻을 왼쪽의 URL:

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

을 위한 응용 프로그램(web)이름:

?HttpRuntime.AppDomainAppVirtualPath
"/"

이와 함께,당신을 추가할 수 있 h 상대 경로로 후에는 취득의 완전한 URL 이 있습니다.

이 작업해야 합니다.수프에 슬래시입니다.가 확실하지 않는 경우에는 그들이 필요는 없습니다.

string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top