2011-09-07 72 views
0

如果有人想下载文件而不是播放它,那么将使用哪些功能或方法。从网址下载mp3文件的方法

我想下载该文件。给我一些例子,如果你可以,或者给我一些关于这个概念的想法。

非常感谢Thankyou。

回答

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]; 

}