php - AFNetworking 将 UIImage 上传为 NSData -> PHP 脚本移动文件

标签 php ios upload afnetworking-2

我知道这个问题被发布了很多次。我试图让我的代码工作,但找不到问题所在。请帮我。 在我的应用程序中,用户拍了一张照片,这应该上传到网络服务器。

这是我的 iOS 代码:

NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"oeffnungszeiten.jpg"], 1.0);

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:BaseURLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    [formData appendPartWithFileData:data name:@"uploadedfile" fileName:_imageString mimeType:@"image/jpeg"];
} error:nil];


AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSProgress *progress = nil;


NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];


[uploadTask resume];

这是我的 PHP 代码:

<?php
error_log("\n-->".$FILES["uploadedfile"]['name'], 3, "log.txt");
$target_path = "/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if (!$FILES["uploadedfile"]) {
    error_log("\nleer", 3, "log.txt");
}
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
    error_log("The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded", 3, "log.txt");
} else{
    echo "There was an error uploading the file, please try again!";
    error_log("There was an error uploading the file, please try again!", 3, "log.txt");
}
?>

你能找到我的错吗?没有图像发送到服务器!

最佳答案

几个想法:

  1. 有些服务器不处理chunked 请求。 (chunkedTransfer-encoding 传统上是一种将请求流式传输到服务器的方式,其中 Content-length 事先未知。)在 NSURLSession 中创建 chunked 请求的方法是在请求中使用 NSInputStream 而不是 NSData 或文件.

    不幸的是,AFNetworking 总是使用 NSInputStream 技术(至少对于多部分请求),这意味着所有请求都使用 Transfer-encoding 流式传输到服务器>分块。不过,您可以在发出请求后,从 NSInputStream 创建一个 NSData,从请求中删除 HTTPBodyStream,然后使用上传使用 NSData 而不是流式请求的任务工厂方法。

    NSMutableURLRequest *request = ... ; // create the request like you are now
    
    // create `NSData` from the `NSInputStream`
    
    NSMutableData *requestData = [NSMutableData data];
    u_int8_t buffer[1024];
    NSInteger length;
    
    [request.HTTPBodyStream open];
    
    do {
        length = [request.HTTPBodyStream read:buffer maxLength:sizeof(buffer)];
        if (length > 0)
            [requestData appendBytes:buffer length:length];
    } while (length > 0);
    
    [request.HTTPBodyStream close];
    
    // now that we have our `NSData`, we can remove `HTTPBodyStream` from request
    
    request.HTTPBodyStream = nil;
    request.HTTPBody = nil;
    
    // instead of uploadTaskWithStreamedRequest, actually specify the `NSData` for the body of the request
    
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromData:requestData progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"%@ %@", response, responseObject);
        }
    }];
    
    [uploadTask resume];
    

    这确实有点低效(您可以通过将其流式传输到文件而不是 NSData 然后在上传任务中使用它来减少内存占用),但这是我唯一的方法知道强制 AFNetworking 不对多部分请求发出流式、分块 请求。因此,我建议仅在您的服务器不接受 chunked 请求时才进行此练习。

  2. 如果您的服务器发出身份验证质询,则 AFNetworking 中存在可能会给您带来问题的错误。 (如果您没有收到任何身份验证质询,则此错误不会为您显示出来,您不必为此担心。)

    Charles 中查看此内容,我注意到 AFNetworking 在请求 header 中正确指定了边界,但是当在身份验证质询后重新发出的请求正文中使用边界时,正文部分的边界变为 nil。这是因为 AFHTTPBodyPartcopyWithZone 中存在错误。我已经发布了 pull request可以解决这个问题。

    如果您想通过转到 AFSerialization.h 并替换 AFHTTPBodyPartcopyWithZone 来在您的本地副本中修复此问题:

    - (id)copyWithZone:(NSZone *)zone {
        AFHTTPBodyPart *bodyPart = [[[self class] allocWithZone:zone] init];
    
        bodyPart.stringEncoding = self.stringEncoding;
        bodyPart.headers = self.headers;
        bodyPart.bodyContentLength = self.bodyContentLength;
        bodyPart.body = self.body;
    
        return bodyPart;
    }
    

    - (id)copyWithZone:(NSZone *)zone {
        AFHTTPBodyPart *bodyPart = [[[self class] allocWithZone:zone] init];
    
        bodyPart.stringEncoding = self.stringEncoding;
        bodyPart.headers = self.headers;
        bodyPart.bodyContentLength = self.bodyContentLength;
        bodyPart.body = self.body;
        bodyPart.boundary = self.boundary;
    
        return bodyPart;
    }
    

    我只是将 boundary 添加到复制对象时必须复制的属性列表中。

  3. 与您的原始问题无关,您有一行内容为:

    NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"oeffnungszeiten.jpg"], 1.0);
    

    这个通过 UIImage 来回传输的过程不会返回与原始文件相同的对象。首先,考虑到您使用的品质因数为 1.0,它可能比您的原始文件大得多。您还会剥离任何元数据(例如拍摄日期、使用的相机、相机设置等)。

    如果这是您的意图,那很好,但通常您只需发送原始 JPEG:

    NSURL *imageFileURL = [[NSBundle mainBundle] URLForResource:@"oeffnungszeiten" withExtension:@"jpg"];
    NSData *data = [NSData dataWithContentsOfURL:imageFileURL];
    

关于php - AFNetworking 将 UIImage 上传为 NSData -> PHP 脚本移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423092/

相关文章:

php - 将亚马逊 MWS 暂存器查询转换为 API 调用

ios - 如何使用 QuartzPDF 在 PDF 中搜索非 ASCII 字符(西里尔字符)?

ios - 通过包含 UIScrollView 的滚动重置 UIViews 的框架

php - 允许用户将图像上传到应用程序相册的 Facebook 应用程序

java - 根据 Controller 更改文件大小限制 (maxUploadSize)

javascript - Angular Input Upload - 获取文件名并上传

php - MySQLi (PHP) 中的绑定(bind)参数和转义字符串

php - 如何组合这些查询以获得更快的性能?

php - 我可以用 file_get_contents() 模拟用户的访问吗

ios - AFNetworking - 发布 http 数据后卡住了