33

我正用这个把头撞在墙上。我想UIImage从库中选择并将其上传到服务器,就像 on 可以用<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)创建上传任务
它们都不适合我,所以我仍在努力解决这个问题,但看不到任何解决方案

4

3 回答 3

94

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

-(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];
}

奇迹般有效 :)

于 2013-11-07T16:57:43.057 回答
2

AFNetworking您可以像这样使用上传图片Swift...

    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)
        })
    }
于 2015-05-16T13:32:29.187 回答
1
 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);
 }];
于 2014-08-18T07:05:48.090 回答