文件未显示在文档目录中?
我想将 PDF 下载到我的文档目录中。 我这样做:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://www.test.com/test.pdf"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[self documentsDirectory]];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished");
NSError *error;
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error];
if (array == nil) {
NSLog(@"array == nil");
}
NSLog(@"Array count: %d", [array count]);
}
- (NSString *)documentsDirectory {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [paths objectAtIndex:0];
}
但我的数组总是空的,我不知道为什么......
它现在可以工作,创建了一个新项目并且它可以工作。 有点奇怪。
I want do download a PDF to my documents directory.
I do it like this:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://www.test.com/test.pdf"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[self documentsDirectory]];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished");
NSError *error;
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error];
if (array == nil) {
NSLog(@"array == nil");
}
NSLog(@"Array count: %d", [array count]);
}
- (NSString *)documentsDirectory {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [paths objectAtIndex:0];
}
But my array is always empty, I don't know why...
It works now, made a new project and it works.
A bit weird.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为编写的代码实际上是用您的文件替换 Documents 目录,而不是将其放入 Documents 目录中。
如果您在 Finder 中查看 Simulator 目录(~/Library/Application Support/iPhone Simulator/4.3/Applications/...),您将看到如下内容:
请注意 Documents 目录的“种类”不再是“文件夹”而是“下载完成后“文档”。
如果您将 .pdf 附加到 Finder 中的文档文件,则它应该是您下载的 PDF。我尝试了你的代码,这就是它为我所做的。您将用下载内容覆盖 Documents 目录。
如果您将显式文件名添加到下载路径,您的代码将起作用。像这样的事情:
如果您查看 HTTPRequest 文档中的 示例,您将看到指定了文件路径,而不仅仅是下载目录。
作为旁注,您还应该实现
- (void)requestFailed:(ASIHTTPRequest *)request
委托方法,并在contentsOfDirectoryAtPath:
数组为nil(在您的requestFinished:
代码中)。这将使您更容易找到问题所在。注意:确保在修复代码之前从模拟器中删除该应用程序,否则当您尝试写入该 Documents 目录时会收到错误,因为它现在不再是目录!
This is happening because the code as written is actually replacing the Documents directory with your file instead of putting it in the Documents directory.
If you look in the Simulator directory in the Finder (~/Library/Application Support/iPhone Simulator/4.3/Applications/...), you'll see something like this:
Notice how the "Kind" of the Documents directory is no longer a "Folder" but a "Document" after your download completes.
If you append .pdf to the Documents file in the Finder, it should be your downloaded PDF. I tried your code and that's what it did for me. You are overwriting the Documents directory with your download.
Your code will work if you add an explicit file name to the download path. So something like this:
If you check out the example in the HTTPRequest documentation, you'll see that a file path is specified, and not just a download directory.
As a side note, you should also implement the
- (void)requestFailed:(ASIHTTPRequest *)request
delegate method and check/print out the NSError when thecontentsOfDirectoryAtPath:
array is nil (in yourrequestFinished:
code). That would have made it easier for you to track down the issue.NOTE: Make sure to delete the app from your simulator before you fix your code, or you'll get an error when you try to write to that Documents directory, as it's now no longer a directory!
我不确定您的 dir 变量来自哪里,但看起来这就是您的问题所在。
您可以像这样正确返回数组:
据我所知,在将数组分配给 *array 之前,您可以正确记录数组,但随后您将使用 dir 变量。也不需要分配 FileManger 对象,因为您可以像分配
*array
时那样简单地使用[NSFileManger defaultManager]
。另外,当你说你的数组为空时,你的意思是
nil
并且你的 array == nil 日志正在触发,或者你正在使用[array count];
并且它返回0?I'm not sure where your
dir
variable is coming from but it looks like this is where your problem lies.You can return the array correctly like so:
From what i can tell your logging the array correctly before you assign it to
*array
but then you are using thedir
variable. There is also no need to alloc a FileManger object as you can simply use[NSFileManger defaultManager]
as you have done when assigning*array
.Also, when you say your array is empty, do you mean
nil
and your array == nil log is firing, or that you are using[array count];
and it is returning 0?