Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 请求失败:不可接受的内容类型:使用AF2.5的图像/jpg_Php_Ios_Json_Afnetworking 2 - Fatal编程技术网

Php 请求失败:不可接受的内容类型:使用AF2.5的图像/jpg

Php 请求失败:不可接受的内容类型:使用AF2.5的图像/jpg,php,ios,json,afnetworking-2,Php,Ios,Json,Afnetworking 2,我知道这个问题在stack overflow中已经被问过好几次了。但我还是很困惑。我试过使用 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"image/jpg", nil]; 但在添加headerContent类型之前,我仍然得到了错误:application/json;在我的php文件中。但是我的php文件中没有jason结构 我的立场是: AFHTTPReq

我知道这个问题在stack overflow中已经被问过好几次了。但我还是很困惑。我试过使用

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"image/jpg", nil];
但在添加headerContent类型之前,我仍然得到了错误:application/json;在我的php文件中。但是我的php文件中没有jason结构

我的立场是:

AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];

NSError *__autoreleasing* error;
// 2. Create an `NSMutableURLRequest`.
NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://192.168.199.118/search.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"uploadedfile"
                            fileName:@"image.jpg"
                            mimeType:@"image/jpg"];
} error:(NSError *__autoreleasing *)error];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"image/jpg", nil];

AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
                                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                     NSLog(@"Success %@", responseObject);


                                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     NSLog(@"Failure %@", error.description);
                                     if (error.code == -1001) {
                                         [self NetworkAlert:@"Resquest timed out. Please try again and check your network connection."];

                                     }
                                 }];
我的php方面是:

<?php

#function for streaming file to client
function streamFile($location, $filename, $mimeType='image/jpg')
{ if(!file_exists($location))
  { header ("HTTP/1.0 404 Not Found");
    return;
  }

  //header("Content-Type: $mimeType"); 
  header("Content-type:application/json");
  readfile($location);

}

#**********************************************************
#Main script
#**********************************************************
header("Content-type:application/json");
#<1>set target path for storing photo uploads on the server
$photo_upload_path = "upload/";
$photo_upload_path = $photo_upload_path.basename( $_FILES['uploadedfile']['name']); 
$photo_upload_indicator_path = "upload/image_ready";

#<2>set target path for storing result on the server
$downloadFileName = 'processed_';
$processed_photo_output_path = "output/processed_";
$processed_photo_output_path = $processed_photo_output_path.basename( $_FILES['uploadedfile']['name']); 
$processed_photo_output_indicator_path = "output/result_ready";
$downloadFileName = $downloadFileName.basename( $_FILES['uploadedfile']['name']); 

#<3>modify maximum allowable file size to 10MB and timeout to 300s
ini_set('upload_max_filesize', '10M');  
ini_set('post_max_size', '10M');  
ini_set('max_input_time', 300);  
ini_set('max_execution_time', 300);  

#<4>Get and stored uploaded photos on the server
if(copy($_FILES['uploadedfile']['tmp_name'], $photo_upload_path)) {

    #<5>signal that the image is ready
    $handle = fopen($photo_upload_indicator_path, 'w');
    fprintf($handle, '%s', $photo_upload_path);
    fclose($handle);

    #<6>wait until the result is ready
    while (!file_exists($processed_photo_output_indicator_path))
    {
        usleep(1000000);
    }
    usleep(1000000);
    unlink($processed_photo_output_indicator_path);

    #<7>stream processed photo to the client
    streamFile($processed_photo_output_path, $downloadFileName,'image/jpg');
} else{
    echo "There was an error uploading the file to $photo_upload_path !";
}

?>
headerContent类型:application/json;帮助我解决这个问题。但我不知道为什么。 如果删除此代码,我将始终收到请求失败错误。如果我想从php服务器接收一些带有图像的信息,哪种方式是最好的?非常感谢你的建议

您的PHP脚本在注释主脚本下方的行中设置内容类型标题

您应该删除此项,并为您的图像响应正确的mime类型,图像/jpeg包含“e”。AFNetworking附带一个响应序列化程序,它将自动将内容类型为image/jpeg的响应解码为UIImage:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];

AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
                                 success:^(AFHTTPRequestOperation *operation, UIImage *responseImage) {
                                     NSLog(@"Success %@", responseImage);


                                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     NSLog(@"Failure %@", error.description);
                                     if (error.code == -1001) {
                                         [self NetworkAlert:@"Resquest timed out. Please try again and check your network connection."];

                                     }
                                 }];