下载文件并保存到内存中

时间:2012-09-21 09:05:44

标签: iphone objective-c ios download

在文本框中,我输入一个网址并将其下载到特定路径并存储该下载文件,我必须先提供该文件的具体位置和文件名。这是有效的,但是当我下载任何其他格式时出现问题(例如.mp4,.avi)它将存储为.mp3

NSString * URL =Textbox.text;
dest_path=[NSHomeDirectory() stringByAppending:@"/Documents/file.mp3"];
//to download url and save to specific path
download=[Downloader Download:URL withTargetPath:dest_path withDelagate:self];

我希望每当我下载它应该从那里格式保存。如果我下载.mp4文件。应该用.mp4格式保存。请提示一些代码..

1 个答案:

答案 0 :(得分:0)

 NSURL *url=[NSURL URLWithString:@"/Documents/file.mp3"];



    NSURLRequest *req=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];

    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    if(con){
        receivedData = [NSMutableData data] ;
    } else {

    } 

使用:

NSURLConnectionDataDelegate,NSURLConnectionDelegate

方法

喜欢:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d{}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {}