[英]AFNetworking 2.0 POST + PHP block failure called although upload is successful
Description of my problem is actually in question header. 我的问题的描述实际上在问题标题中。 I'm new with web technologies, so this behaviour is for me a little striking.
我是Web技术的新手,所以这种行为对我来说有点惊人。 Why failure block is called although image upload has been completed with success?
尽管成功完成了图像上传,但为什么会调用失败块? Moreover I searched an answer for my question on SoF and applied some solutions, but this error still is appearing.
此外,我在SoF上搜索了我的问题的答案并应用了一些解决方案,但是此错误仍然出现。 Please help if possible.
如果可以的话请帮忙。 Thank you in advance.
先感谢您。
OBJECTIVE - C: 目标-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: 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!";
}
?>;
I'm getting this error: 我收到此错误:
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;
It seems that the problem is in the fact that you are using AFJSONResponseSerializer
to serialize the response of your PHP upload script. 看来问题出在您正在使用
AFJSONResponseSerializer
来序列化PHP上传脚本的响应。
And your PHP is not returning a JSON - thus serializer throws an error. 而且您的PHP没有返回JSON-因此序列化器将引发错误。
You could either modify PHP script so that it returns a JSON formatted result. 您可以修改PHP脚本,以使其返回JSON格式的结果。 Or you can use different type of serializer.
或者您可以使用其他类型的序列化器。
According to this forum solution would be to use [AFResponseSerializer serializer]
- i'm not sure if it even exists. 根据这个论坛的解决方案将是使用
[AFResponseSerializer serializer]
-我不确定它是否存在。 Try basic AFHTTPResponseSerializer
. 尝试基本的
AFHTTPResponseSerializer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.