使用ASIHTTPRequest下载多个文件

时间:2012-11-05 14:39:20

标签: ios file download request asihttprequest

我正在开发一个项目,我需要为服务器下载3个不同的.plist文件,但我只能设法下载其中一个.plist文件。有谁知道如何下载所有3个?我正在使用ASIHTTPRequest。 mycode的:

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

}

我认为我走在正确的轨道但不确定......干杯!

1 个答案:

答案 0 :(得分:3)

您在多个地方使用了相同的变量名来获取三个文件。第一个变量是“filePath”

将变量更改为单独的名称,例如像这样,

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

同样是“request”变量??

修正例如像这样,

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

或者,如果您只想拥有1个请求对象,那么可以尝试遍历所有3个URL并逐个分配请求。 确保在循环中提供不同的filePaths

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