将文件下载到iPhone中的文件夹 [英] Download file to Folder in iPhone

查看:143
本文介绍了将文件下载到iPhone中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone新手,

I am new to iPhone,

我制作了应用程序,当用户下载任何内容时,它会被下载到 DocumentDirectory

I made app in which, when user downloads anything it gets downloaded into DocumentDirectory

我想在我的文件夹中下载我在 DocumentDirectory

I wants downloading inside my folder which i have created in DocumentDirectory

如何实现这一目标?

我以为我会将文件下载到我的 DocumentDirectory 我会将该文件移到 DocumentDirectory 内的文件夹中。

I thought I will download file to my DocumentDirectory and i will move that file to my folder inside DocumentDirectory.

这是好的做法吗?

Is it good practice ?

这是我的代码片段但我仍然无法移动我的文件。

Here is my code snippet, But still i am unable to move my file.

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

    NSString *file = [NSString stringWithString:[url absoluteString]];
    NSURL *fileURL = [NSURL URLWithString:file];

    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    [NSURLConnection connectionWithRequest:req delegate:self];


    NSString *DestPath=[[[SourcePath stringByAppendingString:@"/"]stringByAppendingString:BookCateg]stringByAppendingString:@"/"];

                   if ([fm copyItemAtPath:SourcePath toPath:DestPath error:&error]) 
                   {
                      NSLog(@"Success !");
                   }
                  else{
                      NSLog(@"Fail to Move!");
                   }

}

我的日志显示无法移动!

我们将不胜感激任何帮助。

推荐答案

您应该直接将文件下载到文档目录的文件夹中,为此您需要创建目录为@iPhone Developer建议并在该目录中写入文件

You should directly download file to folder of document directory and for that u need to create directory as @iPhone Developer suggested and write file in that directory

例如:

  NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/File"];
  error = nil;
  if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

  NSURL *url = [NSURL URLWithString:@"http://www.ndqatar.mobi/18December/admin/rington/133/QatarDay13.mp3"];
  NSData *data = [NSData dataWithContentsOfURL:url];
  if(data)
  {
    stringPath = [stringPath stringByAppendingPathComponent:[url lastPathComponent]];
    [data writeToFile:stringPath atomically:YES];
  }

如果你想要你可以移动这样的文件:

If u want u can move file like this:

 NSString *destPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Dest"];
  error = nil;
  if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:destPath withIntermediateDirectories:NO attributes:nil error:&error];
  destPath = [destPath stringByAppendingPathComponent:[stringPath lastPathComponent];];

 [[NSFileManager defaultManager] copyItemAtPath:stringPath toPath:destPath error:&error];

这篇关于将文件下载到iPhone中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆