【问题标题】:Uploading image with AFNetworking 2.0使用 AFNetworking 2.0 上传图像
【发布时间】:2013-11-19 03:03:31
【问题描述】:

我正用这个把头撞到墙上。我想从库中选择UIImage 并将其上传到服务器,就像使用<form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data"> 一样。我得到了这个错误,而不是成功:

error = Error Domain=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa 错误 3840。)”(JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。) UserInfo=0x145e5d90 {NSDebugDescription=JSON 文本不以数组或对象开头,并允许未设置片段的选项。}

我试过这样:

-(void)uploadPhoto{
NSString *path = @"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    if(self.imageView.image){
        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
    }
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error = %@", error);
}];

[self blockView:self.view block:YES];
}

但它不工作...服务器说没有文件。不确定加密是错误的、mime 类型还是什么?

也试过这个:

    [manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error = %@", error);
}];

还有这个:

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFormData:imageData name:@"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
    [self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error response.object = %@", operation.responseObject);
    [self blockView:self.view block:NO];
}];

没有任何工作。希望有人可以提供帮助,因为我一直坚持下去,并在 SO
tia

编辑:新尝试
1) 首先是多部分形式
2) 创建上传任务
他们都没有为我工作,所以我仍在努力解决这个问题,但看不到任何解决方案

【问题讨论】:

  • 失败块上[操作响应字符串]的日志是什么?
  • 你的意思是第一个例子?它是来自服务器的 html 文件,其中包含错误信息(来自coldfusion)
  • 其实真的很乱,但从中我可以找到诊断部分说:表单字段照片不包含文件
  • 不知道这个参数有什么问题 [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];

标签: ios objective-c file-upload uiimageview afnetworking-2


【解决方案1】:

我不确定是哪一部分(我认为缺少一些细节)负责,但我终于做到了:)给你:

-(void)uploadPhoto{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
    NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
    NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];
}

像魅力一样工作:)

【讨论】:

  • 服务器url的其余部分,注意base url在AFHTTPRequestOperationManagerinit方法中
  • “[op start];”需要什么?我以与上述相同的方式执行此操作,但链接返回成功,但文件未上传到服务器。
  • [操作开始]; -->开始上传。
  • @raistlin 嘿,这段代码对我有用,但与会话管理器相同的代码不起作用。也许您知道为什么会发生这种情况?
  • 名字是什么:paramNameForImage你能帮帮我吗
【解决方案2】:
 UIImage *image = [UIImage imageNamed:@"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);


NSString *queryStringss = [NSString stringWithFormat:@"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];


[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {


     [formData appendPartWithFileData:imageData name:@"fileName" fileName:@"decline_clicked.png" mimeType:@"image/jpeg"];



 }
      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {



    NSDictionary *dict = [responseObject objectForKey:@"Result"];

    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);


 }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {

     NSLog(@"Error: %@ ***** %@", operation.responseString, error);
 }];

【讨论】:

    【解决方案3】:

    您可以像这样使用Swift 上传带有AFNetworking 的图像...

        let compression = 0.5
    
        let imageData = UIImageJPEGRepresentation("image", CGFloat(compression))
    
    if imageData != nil{
       var manager = AFHTTPRequestOperationManager()
            manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html", "application/json"]) as Set<NSObject>
    
            var dictParams = [
            "familyId":"10000",
            "contentBody" : "Some body content for the test application",
            "name" : "the name/title",
            "typeOfContent":"photo"
        ]
    
            let url = "http://...."
            manager.POST(url, parameters: dictParams, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
                //code
                formData.appendPartWithFileData(imageData, name: "file", fileName: "filename", mimeType: "image/png")
                }, success: { (operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in
                    println(responseObject)
                }, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
                    println(error)
            })
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多