我正在使用 Zendesk 开发 iOS 应用程序,我正在使用 REST v2 api,但评论附件有问题。发送附件的操作看起来不错,但是当尝试从评论中读取附件时,我遇到了问题,因为文件已损坏(我正在发送图像)。我正在使用 AFNetworking 库。这是我的代码:
- (void)addAttachment:(NSData*)data withFileName:(NSString*)fileName {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:API_USER password:API_TOKEN];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
[manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
NSDictionary *parameters = @{@"image":@{ @"content_type": @"image/jpeg", @"filename":fileName, @"file_data": [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}};
[manager POST:[NSString stringWithFormat:@"%@uploads.json?filename=%@", API_URL, fileName] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dictionary = responseObject;
if (dictionary != nil && [dictionary objectForKey:@"upload"] != nil) {
NSString *token = [[dictionary objectForKey:@"upload"] objectForKey:@"token"];
if ([self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:YES andToken:token];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
if ([self.delegate respondsToSelector:@selector(didFinishedAddAttachmentWithSuccess:andToken:)]) {
[self.delegate didFinishedAddAttachmentWithSuccess:NO andToken:nil];
}
}];
}
有什么建议么?