ASIHTTPRequest - 下载问题

时间:2011-07-05 19:02:11

标签: iphone xcode download asihttprequest

我尝试从我的服务器下载一个文件,这是代码,在我的控制台上我看到了xml文件,但我无法保存。 问题出在哪里?

- (IBAction)grabURL:(id)sender{

    NSURL *url = [NSURL URLWithString:@"http://www.endurodoc.net/photo/data.xml"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];

    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"%@",response);

    }
    else{
        NSLog(@"Errore");
    }    

    //[request setDownloadDestinationPath:@"/Users/kikko/Desktop/data.xml"];

    // SAVED PDF PATH
    // Get the Document directory
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your saved pdf location
    NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"data.xml"];

    // TEMPORARY PDF PATH
    // Get the Caches directory
    NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // Add your filename to the directory to create your temp pdf location
    NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:@"data.xml"];

    // Tell ASIHTTPRequest where to save things:
    [request setTemporaryFileDownloadPath:tempPdfLocation];     
    [request setDownloadDestinationPath:pdfLocation]; 

}

1 个答案:

答案 0 :(得分:3)

你需要把:

[request setTemporaryFileDownloadPath:tempPdfLocation];     
[request setDownloadDestinationPath:pdfLocation]; 

之前:

[request startSynchronous];

ASIHTTPRequest在请求发出时保存文件,因此如果在请求发生后设置了这些属性,则不会发生任何事情。