ios AFNeworking 3.0 上傳圖片

entper 8年前發布 | 19K 次閱讀 Objective-C開發

來自: 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了 

 本文由用戶 entper 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!