-1

私はurlという名前の文字列を持っています

NSString *url = @"http://sound17.mp3pk.com/indian/barfi/%5BSongs.PK%5D%20
              Barfi%20-%2001%20-%20Barfi!.mp3";

今、私はこの曲をダウンロードしています。これを保存するには、ファイル名を指定する必要があります%5BSongs.PK%5D%20 Barfi%20-%2001%20-%20Barfi!.mp3。 url ですが、URL フォーマッタで構成することはできません。

私がしていることは

Dest_path=[NSHomeDirectory() stringByAppendingString:@"/Documents/a3"];

result =[Dest_path stringByAppendingString:@".mp3"];
helper = [DownloadHelper download:url withTargetPath:result withDelegate:self];
4

1 に答える 1

1

このようなものが動作するはずです:

NSString *URLString = @"http://sound17.mp3pk.com/indian/barfi/%5BSongs.PK%5D%20Barfi%20-%2001%20-%20Barfi!.mp3";
NSURL *URL = [NSURL URLWithString:url];
NSString *filename = [[[URL path] lastPathComponent] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//=> [Songs.PK] Barfi - 01 - Barfi!.mp3

コンテンツが動的な場合 (たとえば のようなもの)、URL だけから常に有用なファイル名を取得できるとは限らないことに注意してくださいhttp://example.com/getsong?q=foobar

編集:任意の URL からファイル名を取得するより良い方法は、リクエストを作成し、レスポンスのContent-Disposition HTTP ヘッダーを検査することです。

于 2012-09-24T12:11:02.520 に答える