이미지를 파싱 이미지 경로
-
12-12-2019 - |
문제
이미지를 동적으로 크기 조정합니다.
ImageJob i = new ImageJob(file, "~/eventimages/<guid>_<filename:A-Za-z0-9>.<ext>",
new ResizeSettings("width=200&height=133&format=jpg&crop=auto"));
i.Build();
.
이미지 상대 URL을 DB에 저장하려고 시도합니다.i.FinalPath
속성은 나에게 다음과 같습니다.
C:\inetpub\wwwroot\Church\eventimages\56b640bff5ba43e8aa161fff775c5f97_scenery.jpg
.
어떻게 이미지 파일 이름을 얻을 수 있습니다 -이 모든 것을 파싱하는 가장 좋은 방법을 얻을 수 있습니까?
원하는 문자열 : /eventimages/56b640bff5ba43e8aa161fff775c5f97_scenery.jpg
해결책
아래에있는 것,
var sitePath = MapPath(@"~");
var relativePath= i.FinalPath.Replace(sitePath, "~");
. 다른 팁
정규 표현식을 사용하십시오
Regex.Match
.
패턴을 만들고 원하는 값을 추출합니다
string input = "C:\\inetpub\\wwwroot\\Church\\eventimages\\56b640bff5ba43e8aa161fff775c5f97_scenery.jpg";
Match match = Regex.Match(input, @"^C:\\[A-Za-z0-9_]+\\[A-Za-z0-9_]+\\[A-Za-z0-9_]+\\([A-Za-z0-9_]+\\[A-Za-z0-9_]+\.jpg)$", RegexOptions.IgnoreCase);
if (match.Success)
{
// Finally, we get the Group value and display it.
string path = match.Groups[1].Value.Replace("\\", "/");
}
. 여기에 유틸리티 방법에서 사용하는 것입니다.
Uri uri1 = new Uri(i.FinalPath);
Uri uri2 = new Uri(HttpContext.Current.Server.MapPath("/"));
Uri relativeUri = uri2.MakeRelativeUri(uri1);
.
(다른 누군가로부터 도난당했습니다 ... 누가 기억할 수 없지만 감사합니다)
제휴하지 않습니다 StackOverflow