2012-01-18 2 views
3

URL에서 이미지를 다운로드하려고합니다. URL 끝에 추가 된 보안 키가 있고 다음 오류가 계속 발생합니다. System.Net.WebException : WebClient 요청 중에 예외가 발생했습니다. ---> System.ArgumentException : 경로에 잘못된 문자가 있습니다.ASP.NET 쿼리 문자열이있는 URL에서 이미지 파일 다운로드

올바른 구문을 잘 모르겠습니다. 여기에 내 코드가있다.

string remoteImgPath = "https://mysource.com/2012-08-01/Images/front/y/123456789.jpg?api_key=RgTYUSXe7783u45sRR"; 
string fileName = Path.GetFileName(remoteImgPath); 
string localPath = AppDomain.CurrentDomain.BaseDirectory + "LocalFolder\\Images\\Originals\\" + fileName; 
WebClient webClient = new WebClient(); 
webClient.DownloadFile(remoteImgPath, localPath); 
return localPath; 
+1

내가 Path.GetFileName' 당신이 그것을하지 생각하지'생각하지 않습니다. –

+0

그것은 지정된 경로 문자열의 파일 이름과 확장명을 반환합니다. 아, 거기도 보지 못했습니다. 로컬 파일 경로에서 쿼리 문자열을 가져 오지 않도록 파일을 분할해야합니다. – user204588

+0

브라이언, 그게 다야. 이봐, 네가 뭔가 분명히 놓칠 때가 있었어. 당신은 당신의 대답을 넣고 싶습니다. 그리고 나는 그것을 표시 할 것입니까? – user204588

답변

8

나는 이것이 당신이 찾고있는 무엇이라고 생각 :

string remoteImgPath = "https://mysource.com/2012-08-01/Images/front/y/123456789.jpg?api_key=RgTYUSXe7783u45sRR"; 
Uri remoteImgPathUri = new Uri(remoteImgPath); 
string remoteImgPathWithoutQuery = remoteImgPathUri.GetLeftPart(UriPartial.Path); 
string fileName = Path.GetFileName(remoteImgPathWithoutQuery); 
string localPath = AppDomain.CurrentDomain.BaseDirectory + "LocalFolder\\Images\\Originals\\" + fileName; 
WebClient webClient = new WebClient(); 
webClient.DownloadFile(remoteImgPath, localPath); 
return localPath; 
+0

굉장한 대답 .. –

관련 문제