2012-01-18 14 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

恐ろしい答え.. –

関連する問題