簡體   English   中英

盡管上傳成功,但調用AFNetworking 2.0 POST + PHP塊失敗

[英]AFNetworking 2.0 POST + PHP block failure called although upload is successful

我的問題的描述實際上在問題標題中。 我是Web技術的新手,所以這種行為對我來說有點驚人。 盡管成功完成了圖像上傳,但為什么會調用失敗塊? 此外,我在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...");
    }];
}

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!";
}
?>;

我收到此錯誤:

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;

看來問題出在您正在使用AFJSONResponseSerializer來序列化PHP上傳腳本的響應。

而且您的PHP沒有返回JSON-因此序列化器將引發錯誤。

您可以修改PHP腳本,以使其返回JSON格式的結果。 或者您可以使用其他類型的序列化器。

根據這個論壇的解決方案將是使用[AFResponseSerializer serializer] -我不確定它是否存在。 嘗試基本的AFHTTPResponseSerializer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM