내 문제에 대한 설명은 실제로 질문 헤더에 있습니다. 나는 웹 기술에 익숙하지 않아서이 행동은 나에게 조금 인상적이다. 이미지 업로드가 성공적으로 완료되었지만 실패 블록이 호출되는 이유는 무엇입니까? 또한 SoF에 대한 내 질문에 대한 답변을 검색하고 몇 가지 솔루션을 적용했지만이 오류는 여전히 나타납니다. 가능한 경우 도와주세요. 미리 감사드립니다.AFNetworking 2.0 POST + PHP 블록 오류 (업로드 성공)
목적 - C :
- (void)upload {
NSString *fileName = [NSString stringWithFormat:@"%ld%c%c.jpg", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"];
NSData *data = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:imagePath]);
NSString *URLString = @"http://myServer/myAppFolder";
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:[NSURL URLWithString:URLString]];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFHTTPRequestOperation *operation = [manager POST:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"uploadedfile" fileName:fileName mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure/%@,/%@", error, operation.responseString); //error comes from here
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"uploading...");
}];
}
upload.php로 :
<?php
$filename="uploaded";
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name'])." has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>;
나는이 오류 받고 있어요 :
Failure/Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8a633e0 {NSDebugDescription=Invalid value around character 0.},/The file 1389046572lb.jpg has been uploaded;
'파일 ...'메시지의 '문자 0 주위의 값이 잘못되었습니다.'라는 메시지는 코코아가 보내지 않은 다른 텍스트가 있음을 나타냅니다. –
왜 AFJSONResponseSerializer를 사용하고 있습니까? PHP가 JSON을 반환하지 않습니다 ...'[AFResponseSerializer serializer]'를 사용해보십시오 ... –
내 .php 파일에서 json_encode로 만든 JSON을 반향하려고했습니다. 그러나 끝에 쓰레기에 관한 오류가있었습니다. 나는 serializer에서 문제가 될 수 있음을 깨달았습니다. 사실 AFHTTPResponseSerializer에 대해서는 실제로 알지 못했습니다. 죄송한데 고마워요! 또한 Marc 대답에 대한 답변 :-) – Neru