2011-09-07 73 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]; 

}