• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php - 请求失败 : unacceptable content-type: image/jpg using AFNetworking 2. 5

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:01:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我知道这个问题已经在堆栈溢出中被问过好几次了。但我仍然对此感到很困惑。我试过用

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

但在我添加 header("Content-type:application/json"); 之前我仍然收到错误消息在我的 php 文件中。但是我的 php 文件中没有 jason 结构。

我的ios端是:

AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];

NSError *__autoreleasing* error;
// 2. Create an `NSMutableURLRequest`.
NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod"OST" 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"];
} errorNSError *__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 !";
}

?>

header ("Content-type:application/json");帮助我解决问题。但我不知道为什么。 如果删除此代码,我将始终收到“请求失败错误”。如果我想从带有图像的 php 服务器接收一些信息,哪种方式是最好的?非常感谢您的任何建议。



Best Answer-推荐答案


您的 PHP 脚本在注释 #Main script

正下方的行中设置了一个 Content-type header

您应该删除它并使用正确的图像类型进行响应,image/jpeg(包括“e”)。 AFNetworking 附带一个响应序列化程序,AFImageResponseSerializer ,它将自动将 Content-type 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."];

                                     }
                                 }];

关于php - 请求失败 : unacceptable content-type: image/jpg using AFNetworking 2. 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27757988/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap