-1

i have a string named url i.e

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

Now I am downloading this song and to save this I need to specify filename.I want the filename in such a way that It should take the file name itself from url.like in this url,It should take %5BSongs.PK%5D%20 Barfi%20-%2001%20-%20Barfi!.mp3 which is the file name in the url but should not be consist of Url formatters.

what I am doing is

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

result =[Dest_path stringByAppendingString:@".mp3"];
helper = [DownloadHelper download:url withTargetPath:result withDelegate:self];
3
  • 2
    The title of your question is not of much help. Would be a good excercise to coin what you want to know in just one phrase. Commented Sep 24, 2012 at 12:09
  • thanx for your suggestions.please check now ..is it helpful to understand! Commented Sep 24, 2012 at 12:16
  • 1
    @PhillipMills I am so going to use that link. Commented Sep 24, 2012 at 12:17

1 Answer 1

1

Something like this should work:

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

Note that you cannot always get a useful filename from just a URL, if the content is dynamic (e.g. something like http://example.com/getsong?q=foobar).

Edit: A better way to get a file name from an arbitrary URL would be to make a request and inspect the Content-Disposition HTTP header of the response.

Sign up to request clarification or add additional context in comments.

3 Comments

You should probably note that there are better ways to get a filename, e.g. HTTP headers.
@RichardJ.RossIII can you give me link or some other information.
@omz on that case what should I do ? or do you have any other alternative to give filename.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.