我的文件上传代码与iOS 7完美配合,但在iOS 8.0.2上失败,我使用afnetworking 2.4.1。任何帮助都非常感谢!
我收到此错误回复:
Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be completed. (NSURLErrorDomain error -1017.)" UserInfo=0x1780fa680 {NSErrorFailingURLStringKey=http://silaob.com/uploadscript.php, NSErrorFailingURLKey=http://silaob.com/uploadscript.php, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSUnderlyingError=0x17804fc00 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1017.)"}
这是我的代码:
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/uploadscript.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePathURL name:@"file" fileName:@"log.txt" mimeType:@"text/plain" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//allow invalid certs
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
manager.securityPolicy = policy;
//end
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if(error)
{
DDLogVerbose(@"uploadDeviceLogsToServer Error: %@", error);
}
else
{
NSDictionary *responseObj = (NSDictionary *)responseObject;
if([[responseObj valueForKey:@"result"] intValue] == 1)
DDLogVerbose(@"Log file %@ UPLOADED",filePathURL);
}
}
}];
[uploadTask resume];