【问题标题】:ASIHTTPRequest - download problemASIHTTPRequest - 下载问题
【发布时间】:2011-07-05 19:02:11
【问题描述】:

我尝试从我的服务器下载一个文件,这是代码,在我的控制台上我看到了 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]; 

}

【问题讨论】:

  • 如果 NSURLConnection 可以在不使用任何第三方代码的情况下完全做到这一点,你为什么要使用 ASIHTTPRequest?

标签: iphone xcode download asihttprequest


【解决方案1】:

你需要输入:

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

之前:

[request startSynchronous];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多