2011-07-05 70 views
0

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

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

} 
+0

如果NSURLConnection完全可以在不使用任何第三方代码的情况下使用ASIHTTPRequest,您为什么要使用ASIHTTPRequest? – bioffe

回答

3

你需要把:

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

前:

[request startSynchronous]; 

ASIHTTPRequest不会在文件保存时提出要求,因此,如果您设置这些属性的要求早已之后发生的事情不会发生。

+0

非常感谢! – kikko088