使用AFNetworking上传照片时出错 [英] Error when uploading photo with AFNetworking

查看:106
本文介绍了使用AFNetworking上传照片时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking上传照片,但收到臭名昭著的请求体流耗尽"错误.

I am uploading a photo using AFNetworking and I am getting the infamous "request body stream exhausted" error.

这是我的代码: (_managerAFHTTPRequestOperationManager)

This is my code: (_manager is a AFHTTPRequestOperationManager)

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

AFHTTPRequestOperation *operation = [_manager POST:address parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Written: %lld", totalBytesWritten);
}];

我同时在使用WiFi和4G/3G的iPhone 5S和iPhone 4上收到错误消息.这个问题是一致的,并且每次都会在上载应该完成的时候发生.奇怪的是,它也可以在这些手机上使用,但是几天前,我突然开始收到错误消息.另外,我的同事在使用相同代码的wifi和4G上的iPhone 5上都没有问题.所有手机均运行iOS 7.

I get the error on both my iPhone 5S and my iPhone 4, both using wifi and 4G/3G. The issue is consistent and occurs every time, just when the upload is suppose to finish. The weird thing is that it use to work on these phones as well, but some days ago I suddenly started getting the error. Also, my colleague has no problems on his iPhone 5, both on wifi and 4G, running the same code. All phones run iOS 7.

我知道有些人在使用3G时会遇到此错误,这种情况下的解决方案是使用throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes delay:(NSTimeInterval)delay方法.但是,这对我来说没有任何影响,并且在wifi和移动设备上都收到了错误消息.

I know that some people are getting this error when on 3G, and the solution in that situation is to use the throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes delay:(NSTimeInterval)delay method. However, this has had no affect in my case, and I get the error both on wifi and mobile.

我可以使用curl从同一网络上的计算机执行上传.

I am able to perform the upload using curl from my computer which is on the same network.

推荐答案

我终于在这个答案中找到了一个解决方案: https://stackoverflow.com/a/21304062/569507

I finally found a solution in this answer: https://stackoverflow.com/a/21304062/569507

但是,我没有像建议的那样对AFHTTPRequestOperation进行子类化,而是简单地在其上(或实际上在其实现NSURLConnectionDataDelegate协议的父类AFURLConnectionOperation上)创建了一个包含connection:needNewBodyStream方法的类别.这就是所需要的.我其余的代码保持不变.

However, in stead of subclassing the AFHTTPRequestOperation as suggested, I simply made a category on it (or actually on its parent class AFURLConnectionOperation which implements the NSURLConnectionDataDelegate protocol) that contains the connection:needNewBodyStream method. This is all that is needed. The rest of my code remains unaltered.

AFURLConnectionOperation + Recover.h

#import "AFURLConnectionOperation.h"

@interface AFURLConnectionOperation (Recover)

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request;

@end

AFURLConnectionOperation + Recover.m

#import "AFURLConnectionOperation+Recover.h"

@implementation AFURLConnectionOperation (Recover)

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
{
    if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
        return [request.HTTPBodyStream copy];
    }
    return nil;
}

@end

这篇关于使用AFNetworking上传照片时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆