0
所以這個問題對我來說根本沒有意義,因爲這段代碼在幾個月前寫完時完美無缺地工作。我甚至對「照片上傳作品」的信息做出了承諾。儘管如此,我昨晚打開了它,無法讓它爲我的生活工作。AFNetworking沒有爲照片上傳設置正確的內容類型
,我不斷收到錯誤是「請求失敗:無法接受的內容類型:text/html的」
這是沒有意義的,因爲我設置了內容類型爲image/JPEG !!!!如果任何人有任何有用的提示,爲什麼這會停止工作,我會非常感謝:) :) :)。代碼如下。
``` - (無效)postPhoto:(NSData的*)ImageToPost forImageUUID:(的NSString *)photoUUID {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://MyURL"]];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"michael" password:@"asdf"];
[manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
NSDictionary *parameters = @{@"name" : @"MyName", @"photo_uid" : photoUUID};
AFHTTPRequestOperation *op = [manager POST:@"/api/photo/" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *fileName = [NSString stringWithFormat:@"Photo_%@.jpg",photoUUID];
[formData appendPartWithFileData:ImageToPost name:@"image" fileName:fileName mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"%f%% Uploaded", round(((float)totalBytesWritten/(float)totalBytesExpectedToWrite) * 100));
}];
[op start];
} ```
在結束時,將照片獲取上傳100%,但然後該錯誤開始說它錯誤的內容類型和照片從未實際上發佈到我的服務器。
請幫助:(
[AFNetworking和POST請求]的可能重複(http://stackoverflow.com/questions/12447725/afnetworking-and-post-request) – mattt 2014-10-13 20:13:20