菜鸟教程小白 发表于 2022-12-12 17:58:08

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


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

<p>目标 - C:</p>

<pre><code>- (void)upload {

    NSString *fileName = timeIntervalSince1970], arc4random_uniform(26) + &#39;a&#39;, arc4random_uniform(26) + &#39;a&#39;];
    NSString *imagePath = [ pathForResource:@&#34;image&#34; ofType:@&#34;jpg&#34;];
    NSData *data = UIImagePNGRepresentation();

    NSString *URLString = @&#34;http://myServer/myAppFolder&#34;;

    AFHTTPRequestOperationManager *manager = [ initWithBaseURL:];

    AFJSONResponseSerializer *responseSerializer = ;
    ;
    ];

    AFHTTPRequestOperation *operation = [manager POST:@&#34;upload.php&#34; parameters:nil constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; formData) {
      ;
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@&#34;Success %@&#34;, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@&#34;Failure / %@, /%@&#34;, error, operation.responseString); //error comes from here
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
      NSLog(@&#34;uploading...&#34;);
    }];
}
</code></pre>

<p>上传.php:</p>

<pre><code>&lt;?php
$filename=&#34;uploaded&#34;;
$target_path = &#34;uploads/&#34;;

$target_path = $target_path . basename( $_FILES[&#39;uploadedfile&#39;][&#39;name&#39;]);

if(move_uploaded_file($_FILES[&#39;uploadedfile&#39;][&#39;tmp_name&#39;], $target_path)) {
    echo &#34;The file &#34;.basename( $_FILES[&#39;uploadedfile&#39;][&#39;name&#39;]).&#34; has been uploaded&#34;;
} else {
    echo &#34;There was an error uploading the file, please try again!&#34;;
}
?&gt;;
</code></pre>

<p>我收到此错误:</p>

<pre><code>Failure / Error Domain=NSCocoaErrorDomain Code=3840 &#34;The operation couldn’t be completed. (Cocoa error 3840.)&#34; (Invalid value around character 0.) UserInfo=0x8a633e0 {NSDebugDescription=Invalid value around character 0.}, /The file 1389046572lb.jpg has been uploaded;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题似乎在于您使用 <code>AFJSONResponseSerializer</code> 来序列化 PHP 上传脚本的响应。</p>

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

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

<p>根据<a href="https://github.com/AFNetworking/AFNetworking/issues/1387" rel="noreferrer noopener nofollow">this forum</a>解决方案是使用 <code></code> - 我不确定它是否存在。尝试基本的 <code>AFHTTPResponseSerializer</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于php - 尽管上传成功,但调用了 AFNetworking 2.0 POST &#43; PHP 阻止失败,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/20960529/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/20960529/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - 尽管上传成功,但调用了 AFNetworking 2.0 POST &#43; PHP 阻止失败