如何在AFMultipartFormData AFNetworking 3中发送参数键

时间:2016-11-07 12:31:59

标签: ios asp.net objective-c multipartform-data afnetworking-3

这就是我发送请求的方式:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    NSError *error;
manager.responseSerializer.acceptableContentTypes = nil;
for(NSData *eachImage in self.fetchedAtt) {
NSString *mystring = @"786";
        NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:eachImage name:@"myImage"];
            [formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
                                        name:@"PracticeCode"];
} error:&error];

        NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
                NSLog(@"%@", error.localizedDescription);
                return;
            }

            NSLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
        }];
        [task resume];
    }
}

有时我会:

  

{&#34; Message&#34;:&#34;将MIME多部分正文部分写入输出时出错   。流#&34; }

//这是500错误 有时我会

  

请求失败:不支持的媒体类型(415)

2 个答案:

答案 0 :(得分:1)

您正尝试在单个多表单请求中发送多个文件。 Multipart并不像这样工作。您将不得不一次使用1个文件进行多个多部分请求。所以考虑你有20个文件。您将创建一个异步任务,一次接收一个文件,上传它,然后再次为下一个文件执行相同的功能。

如果你想保留引用,你应该从后端返回文件id,然后将所有id附加到单个数组中,并将该数组作为参数发送到单独的api请求中,以将文件与任何文件链接起来宾语。希望这是有道理的。

答案 1 :(得分:0)

试试这个Afnetworking 3.0

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString: [NSString stringWithFormat:@"%@/API/Upload",BaseURLString]; parameters:@{@"text":@"name"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

         [formData appendPartWithFileData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageurl]] name:@"bizcard" fileName:@"Businesscard.jpg" mimeType:@"image/jpeg"]; // you file to upload
        } error:nil];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                      });
                  }
                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      if (error) {
                          NSLog(@"Error: %@", [[NSString alloc]initWithData:[[error valueForKey:@"userInfo"] valueForKey:@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]);
                          callback(NO,nil);
                          [UtilityClass removeActivityIndicator];

                      } else {
                          callback(YES,responseObject);
                      }
                  }];

    [uploadTask resume];