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

php - 尽管上传成功,但调用了 AFNetworking 2.0 POST + PHP 阻止失败

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

我的问题的描述实际上是有问题的标题。我是网络技术的新手,所以这种行为对我来说有点引人注目。图片上传成功,为什么会调用失败 block ?此外,我在 SoF 上搜索了我的问题的答案并应用了一些解决方案,但仍然出现此错误。如果可能,请提供帮助。提前谢谢你。

目标 - 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...");
    }];
}

上传.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;



Best Answer-推荐答案


问题似乎在于您使用 AFJSONResponseSerializer 来序列化 PHP 上传脚本的响应。

而且您的 PHP 没有返回 JSON - 因此序列化程序会引发错误。

您可以修改 PHP 脚本,使其返回 JSON 格式的结果。或者您可以使用不同类型的序列化程序。

根据this forum解决方案是使用 [AFResponseSerializer serializer] - 我不确定它是否存在。尝试基本的 AFHTTPResponseSerializer

关于php - 尽管上传成功,但调用了 AFNetworking 2.0 POST + PHP 阻止失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20960529/

回复

使用道具 举报

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

本版积分规则

关注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