从url下载mp3文件的方法

时间:2011-09-07 09:46:46

标签: iphone

如果有人想下载文件而不是播放文件,那么将使用哪种功能或方法。

我想下载该文件。如果你能,请给我一些例子,或者让我对这个概念有所了解。

非常感谢。

1 个答案:

答案 0 :(得分:1)

按照以下方式发送请求。

NSURL *url = [NSURL URLWithString:[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setValue:[NSString stringWithFormat:@"bytes=%ld-",0] forHTTPHeaderField:@"Range"];
[theRequest addValue: @"pdf" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
webData = [[NSMutableData alloc] init];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

实施以下方法......

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; }

-(void)connectionDidFinishLoading:(NSURLConnection )connection {
/
******CODE FOR WRITING FILE*************/ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentPath = [paths objectAtIndex:0];

NSString *directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:fileDate]];

[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:NO attributes:nil error:nil];

[webData writeToFile:fileName atomically:YES];

}

NSString *documentPath = [paths objectAtIndex:0]; NSString *directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:fileDate]]; [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:NO attributes:nil error:nil]; [webData writeToFile:fileName atomically:YES];