使用AFNetWorking上传内容+参数,
1 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 2 [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; 3 } error:nil]; 4 5 AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 6 7 NSURLSessionUploadTask *uploadTask; 8 uploadTask = [manager 9 uploadTaskWithStreamedRequest:request 10 progress:^(NSProgress * _Nonnull uploadProgress) { 11 // This is not called back on the main queue. 12 // You are responsible for dispatching to the main queue for UI updates 13 dispatch_async(dispatch_get_main_queue(), ^{ 14 //Update the progress view 15 [progressView setProgress:uploadProgress.fractionCompleted]; 16 }); 17 } 18 completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 19 if (error) { 20 NSLog(@"Error: %@", error); 21 } else { 22 NSLog(@"%@ %@", response, responseObject); 23 } 24 }]; 25 26 [uploadTask resume];
上传成功,但是返回错误信息:
1 Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x14a8ceb90> { URL: http://www.test.com/upload.html } { status code: 200, headers { 2 "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; 3 Connection = "keep-alive"; 4 "Content-Length" = 272; 5 "Content-Type" = "text/html;charset=utf-8"; 6 Date = "Tue, 05 Jan 2016 03:10:06 GMT"; 7 Expires = "Thu, 19 Nov 1981 08:52:00 GMT"; 8 Pragma = "no-cache"; 9 "Set-Cookie" = "SERVERID=8ea4a47cd3afa7bf20d29833f1a0f41f|1451963396|1451963392;Path=/"; 10 "X-Powered-By" = "PHP/5.5.9"; 11 } }, NSErrorFailingURLKey=http://www.c-cam.cc/index.php/Api/Uploadphoto/upload.html, com.alamofire.serialization.response.error.data=<5b312c22 68747470 3a5c2f5c 2f696d67 6363616d 2e632d63 616d2e63 635c2f69 6d616765 5c2f5c2f 616c6275 6d5c2f32 30313630 3130355c 2f616230 38613039 36303134 30393635 32323834 38306135 39666665 35623366 642e6a70 67222c22 68747470 3a5c2f5c 2f696d67 6363616d 2e632d63 616d2e63 635c2f69 6d616765 5c2f5c2f 73686172 655c2f32 30313630 3130355c 2f616230 38613039 36303134 30393635 32323834 38306135 39666665 35623366 642e6a70 67222c22 68747470 3a5c2f5c 2f777777 2e632d63 616d2e63 635c2f69 6e646578 2e706870 5c2f4669 7273745c 2f576f72 6b5c2f70 686f746f 5f646574 61696c5c 2f776f72 6b69645c 2f313434 38342e68 746d6c22 2c22225d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
之前遇到过,又跳进了老坑,答案在这里。
只需要在AFURLResponseSerialization.m的acceptableContentTypes中,添加text/html类型。
1 ... 2 3 - (instancetype)init { 4 self = [super init]; 5 if (!self) { 6 return nil; 7 } 8 9 self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]; 10 11 return self; 12 } 13 14 ....
参考:http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-using-afnetworking-2-0
时间: 2024-10-24 01:51:25