2012-11-05 64 views
1

我正在开发一个项目,我需要为服务器下载3个不同的.plist文件,但我只能设法让其中一个下载。任何人都知道如何让他们全部下载?我正在使用ASIHTTPRequest。 mycode的:使用ASIHTTPRequest下载多个文件

- (void)downloadPlist { 
NSLog(@"Download in progress..."); 


progressView.alpha = 1.0; 


// Here we're downloading the .plist file from a server to the app's Documents Directory. 
// Create file manager 
fileManager = [NSFileManager defaultManager]; 

// Point to Document directory 
documentsDirectory = [NSHomeDirectory() 
         stringByAppendingPathComponent:@"Documents"]; 


documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

filePath = [documentsDirectory stringByAppendingPathComponent:@"example1.plist"]; 
filePath = [documentsDirectory stringByAppendingPathComponent:@"example2.plist"]; 
filePath = [documentsDirectory stringByAppendingPathComponent:@"example3.plist"]; 




// files from server. 
NSURL *url = [NSURL URLWithString:@"http://myWeb.com/example1.plist"]; 
NSURL *url1 = [NSURL URLWithString:@"http://myWeb.com/example2.plist"]; 
NSURL *url2 = [NSURL URLWithString:@"http://myWeb.com/example3.plist"]; 



request = [ASIHTTPRequest requestWithURL:url]; 
request = [ASIHTTPRequest requestWithURL:url1]; 
request = [ASIHTTPRequest requestWithURL:url2]; 
[request setShowAccurateProgress:YES]; 
[request setDownloadDestinationPath:filePath]; 
[request setDownloadProgressDelegate:progressView]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

}

我觉得我在正确的轨道上,但不知道...干杯!

回答

3

你在几个地方使用了相同的变量名来获得三个文件。第一个变量是“文件路径”

更改变量以区分不同的名称。像这样,

filePath1 = [documentsDirectory stringByAppendingPathComponent:@"example1.plist"]; 
filePath2 = [documentsDirectory stringByAppendingPathComponent:@"example2.plist"]; 
filePath3 = [documentsDirectory stringByAppendingPathComponent:@"example3.plist"]; 

与“请求”变量相同?

对例如这样,

request1 = [ASIHTTPRequest requestWithURL:url]; 
request2 = [ASIHTTPRequest requestWithURL:url1]; 
request3 = [ASIHTTPRequest requestWithURL:url2]; 

或者,如果你想有只1请求对象的话,也许尝试过所有3个网址循环,并通过一个指定的要求之一。 确保你在你的循环中提供不同的filePaths

[request setDownloadDestinationPath:filePath**{N}**]; 
+0

刚刚发布我的哈哈。但是,是的,覆盖实体的简单错误。 humayun循环的想法是一个更好的主意。也许用NSDictionaries设置一个NSArray来将URL和文件路径保存在一起。然后循环。 – mashdup