【问题标题】:Unable to download file to app directory in iPhone无法将文件下载到 iPhone 中的应用程序目录
【发布时间】:2012-08-20 10:04:57
【问题描述】:

我是 iPhone 新手,

我目前正在开发一个 iPhone 应用程序,并希望实现从 url 下载文件的功能。我创建了UIWebView,当我单击webview 下载中的download 链接时,将开始下载,我将该文件保存到文档目录中的指定文件夹中。但我无法看到我下载的文件。

这是我的代码 sn-p,

//CAPTURE USER LINK-CLICK in UIwebView.

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

         // Create the request.
            NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:DUrl]
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                  timeoutInterval:60.0];

            // create the connection with the request and start loading the data
            NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
            if (theConnection) {
                // Create the NSMutableData to hold the received data.
                receivedData = [[NSMutableData data] retain];
            } else {
                NSLog(@"Inform the user that the connection failed."); 
            }
  return YES; 
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
    [receivedData appendData:data1];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    DirPath=[self MyApplicationDocumentDirectory];
    [receivedData writeToFile:DirPath atomically:YES];

    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
                                                         message:nil delegate:nil 
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
    [Alert show];
    [Alert release];


    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

我们将不胜感激。

编辑:

            BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:MyDirPath];

            if (success) 
            {
                    UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:@"Already downloaded."
                                                                         message:@"Do you want to Downlaod again ?" delegate:self 
                                                               cancelButtonTitle:nil
                                                               otherButtonTitles:@"Yes",@"No",nil];
                    [innerAlert show];
                    [innerAlert release];
            }

在哪里写这个条件?

【问题讨论】:

  • writeToFile:atomically 方法是否返回 YES?目前你只是假设它成功了。您可能应该专门对其进行测试。例如if([receivedData writeToFile:DirPath atomically:YES]) { //alert success } else { //handle failure }
  • 是的,它返回我忘了在这里添加。
  • 好的,你是不是在生成路径的时候也指定了文件名?

标签: iphone ipad uiwebview nsurlrequest writetofile


【解决方案1】:

编辑在写入(保存)下载数据之前检查下载的文件是否已经存在于 doc 目录中,如下所示:

 NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:DirPath error:nil];
 BOOL fileExists = NO;
 for(NSString *fileName in dirContents)
 {
   NSString *filePath = [DirPath stringByAppendingPathComponent:fileName];
   NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    if([receivedData isEqualToData:fileData]) //your receivedData here
    {
        fileExists = YES;
    }
 }
 if(fileExists)
 {
   NSLog(@"File exists");
 }
 else
 {
   NSLog(@"File  does not exists");
  }

你忘记提供用于写入数据的文件名:

DirPath=[self MyApplicationDocumentDirectory];
  NSString *filePath = [DirPath stringByAppendingPathComponent:@"yourFileName"];
[receivedData writeToFile:filePath atomically:YES];

【讨论】:

  • 它的工作,你总是摇滚王子,你给了我的问题很多答案:)
  • 如何显示重复下载警报?意味着如果用户再次下载相同的东西,那么我想显示警报。看看我的编辑在哪里写我的编辑?
猜你喜欢
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多