ios AFNeworking 3.0 上传图片

entper 8年前

来自: http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-us...


按照官方文档的说法

先引入如下

#import "AFURLSessionManager.h"  #import "AFHTTPSessionManager.h"


然后就可以直接把上传demo复制过来

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];NSURLRequest *request = [NSURLRequest requestWithURL:URL];NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {    if (error) {        NSLog(@"Error: %@", error);      } else {        NSLog(@"Success: %@ %@", response, responseObject);      }  }];  [uploadTask resume];

但是 发现上传失败 出现如下错误

NSLocalizedDescription=Request failed: unacceptable content-type: text/html}


这个时候 再服务端 添加

if(!headers_sent() ) { header('Content-Type: application/json'); }


就ok了