ios中的多部分表单/数据上载

时间:2014-05-13 07:15:44

标签: afnetworking multipartform-data

<form method="post" action="upload.php?u=test&p=test" name="FrmUpload"  autocomplete="off" enctype="multipart/form-data">
<input type="file" name="file" id="file" onchange="document.forms[0].submit();"/>
</form>

上面的代码在html中工作。我正在尝试在AFnetworking多部分音频文件上传:这是我的等效ios代码:

NSDictionary *dic = @{@"id":@"file"};

[[RequestOperationManager sharedManager] POST:@"my url string" parameters:dic
 constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        NSError *error = nil;

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"my file path"] name:@"file" fileName:file mimeType:@"audio/aac" error:&error];

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

    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
            NSLog(@"%d : %@", error.code, error.description);
        }
    }];

它返回错误:

错误Domain = NSCocoaErrorDomain Code = 3840“操作无法完成。(Cocoa error 3840.)”(JSON文本没有以数组或对象开头,也没有允许片段未设置的选项。)

1 个答案:

答案 0 :(得分:0)

- (无效)uploadAudio { 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];

}

我认为必须遗漏一些细节......比如你的网址......或关键.....