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];
开发者_StackOverflow中文版 [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.
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:
NSString *filePath = [[[self documentsDirectory] stringByAppendingPathComponent:@"localFile"] stringByAppendingPathExtension:@"pdf"]];
[request setDownloadDestinationPath:filePath];
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 the contentsOfDirectoryAtPath:
array is nil (in your requestFinished:
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!
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:
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error];
From what i can tell your logging the array correctly before you assign it to *array
but then you are using the dir
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?
精彩评论