使用 iOS SDK 上传多个文件并在完成后在本地删除它们

Uploading multiple files with iOS SDK and deleting them locally when done

我遇到以下情况:在我的 DocumentsDirectory 中有用户创建的 pdf 文件。现在我想将它们全部上传到网络服务器。成功完成后,我想从设备中删除文件。

我的第一个问题是,我不知道特定文件的上传成功时间。此外,我在某些文件上遇到错误。

首先是一些代码:

-(void)uploadPDFs
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:docsDir error:nil];

    for (NSString *filename in contents) {
        if([filename.pathExtension isEqualToString:@"pdf"])
        {
            NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:filename]];

            NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
            [config setTimeoutIntervalForResource:10];
            NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/api/putPdf?name=%@&folder=", [defaults objectForKey:@"serverAdresse"], filename]];
            NSData *pdfData = [NSData dataWithContentsOfFile:docsDirURL.path];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
            [request setValue:@"application/pdf" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPMethod:@"POST"];
            [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[pdfData length]] forHTTPHeaderField:@"Content-Length"];
            [request setTimeoutInterval:60];
            // UploadTask is a NSURLSessionUploadTask
            self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:pdfData];

            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

            [self.uploadTask resume];
        }
    }
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });

    if (!error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"NO ERROR");
            // CAN I GET HERE THE UPLOADED FILENAME?
    });
    } else {
        NSLog(@"ERROR: %@", error);
    }
}

我遇到的错误,尤其是当有很多上传时:

(-[ViewController URLSession:task:didCompleteWithError:]) (ViewController.m:4025) ERROR: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fa3957028f0 {NSErrorFailingURLKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=}

使用 NSURLSessionUploadTask 的 taskDescription 属性。 ` 例如;

task.taskDescription = filePath;

`

错误是由于超时时间太短造成的。我增加了这个,问题就消失了。