开发者

download multiple files on iphone

开发者 https://www.devze.com 2023-02-17 08:16 出处:网络
i want to download file(s) from web server. Scenario: As user will select row (file name- user can select 1 or more files to download) and and press download, i am getting URL for each file(all have

i want to download file(s) from web server.

Scenario: As user will select row (file name- user can select 1 or more files to download) and and press download, i am getting URL for each file(all have different URL) and storing into pathArray. and doing following

-(void)downloadFile    {  
    for (int i = 0; i<[pathArray count]; i++) {    
        NSURL *fileURL = [NSURL fileURLWithPath:[pathArray objectAtIndex:i]];  

        NSString *ResultURL = [fileURL absoluteString];  
        NSURL *url = [[NSURL alloc] initWithString:ResultURL];   
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL: url   
        cachePolicy: NSURLRequestReloadIgnoringCacheData  timeoutInterval: 60.0];   
        conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];  

        if (conn)   {
          NSLog(@"*************CONNECTED************");  
          webData = [[NSMutableData data] retain];  
          NSLog(@"weblength : %d : ", [webData length]);  
          downloadTag = YES;  
        } else {
            NSLog(@"*************Connection NOT DONE************");  
            downloadTag=NO;  
        }  
    }  
}


-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  {  
    NSLog(@"*************Try to receive data************");  
    [webData appendData:data];  
    NSLog(@"weblength : %d : ", [webData length]);  
    NSLog(@"*************Data Received an append too ***********");  
    if (downloadTag == YES)  {  
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        self.documentsDir = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"NewResult.zip" ];  
        [[NSFileManager defaultManager] createFileAtPath:documentsDir 开发者_JS百科contents:nil attributes:nil];  
        NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: documentsDir];  
        [file1 writeData: webData];  
        NSLog(@"Webdata : %d",[webData length]);  
        [file1 closeFile];  
    }  
}    

if i am not using for loop i.e only one file download then it work fine but not with for loop....its really important for me to solve this issue.

thank you very much


take a look at ASIHTTPRequest, it has a nice queue for downloading and monitoring progress http://allseeing-i.com/ASIHTTPRequest/How-to-use


You're using asynchronous downloads, but the delegate for each download item is the same object, causing all of the downloaded data to get appended to the same file. The way I do it is to have a small Object (DownloadQueueItem) that is the delegate for a single download. When you download another file, you create a new DownloadQueueItem and it handles everything.


Edit:

Q: instead of %i putting by my self i am searching is there any way to create new file every time like if ""NewResult.zip" is exist then create "NewResult1.zip" and so on

A: You could do something like this:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = @"NewResult%@.zip";
for (int i = 0; ; i++) {
    NSString *filename = NULL;

    if (i == 0) {
        filename = [NSString stringWithFormat:filename, @""];
    }
    else {
        filename = [NSString stringWithFormat:filename, [NSString stringWithFormat:@"%i", i]];
    }

    if (![[NSFileManager defaultManager] fileExistsAtPath:filename]) {
        // Save file.
        break;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号