1
下面是代碼我有下載的存檔,並保存到我的文檔文件夾:保存歸檔的文檔文件夾
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:resource] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
NSData *receivedData;
NSURLResponse *requestResponse;
NSError *requestError;
int i = 3;
while (i) //Try 3 times
{
receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&requestError];
if (receivedData)
{
i = 0;
}
else i--;
}
if (!receivedData)
{
NSLog(@"************************************************");
NSLog(@"** Error downloading data for resource : %@ **", resource);
NSLog(@"************************************************");
}
else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents folder
NSString *dataPath;
dataPath = [documentsDirectory stringByAppendingPathComponent:LIST_FOLDER];
NSError *documentError;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) //Create folder
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&documentError];
}
NSLog(@"saveError = %@", documentError); //Prints out null so no problem here
NSError *writeError;
[receivedData writeToFile:dataPath options:NSDataWritingAtomic error:&writeError];
NSLog(@"write error = %@", writeError);
最後的NSLog打印出以下內容並不會保存歸檔:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" "The operation couldn’t be completed. Is a directory"}
在Documents目錄中的子文件夾被創建,但它是空的。
謝謝
我想將文件保存他們的名字(不一定是同一個),我不能讓他們從檔案? –
另外,我知道NSURLConnection的可以下載它們時我的文檔解壓縮文件。我怎樣才能做到這一點? –