使用AFnetwork上传图像3 Multipart获取nil参数

时间:2017-08-28 11:20:49

标签: objective-c parameters image-uploading afnetworking-3

目前,我正在开展一个项目。我使用afnetwork上传带参数的图像。它在wifi上工作正常,但如果我使用蜂窝网络。它没有正确传递参数。就像几次后端服务器接收参数一样。有时会失踪。我正在使用的代码是

NSString *URLString = [NSString stringWithFormat:@"%@%@",BASEURL,urlID]; // some URL
NSLog(@"URL : %@ Parameters %@",URLString,postVars);

AFHTTPSessionManager *uploadManager = [AFHTTPSessionManager manager];

[uploadManager POST:URLString parameters:postVars constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    NSLog(@"Parameters %@",postVars);

    if (profileImageData) {
        [formData appendPartWithFileData:profileImageData name:@"photo" fileName:@"myimage.jpg" mimeType:@"image/jpeg"];
    }
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"Success %@", responseObject);
    if ([[responseObject valueForKey:@"status"]isEqualToString:@"success"]) {
        block(responseObject,nil);
    }
    else{

        block(responseObject,nil);
    }


} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    if([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:AFNETWORK_OFFLINE_MESSAGE])
    {
        [alertHelper showAlertwithTitle:NOINTERNET_TITLE details:NOINTERNET_DETAILS];
    }
    block(nil,error);
}];

使用在wifi上工作但在蜂窝网络上的参数上传图像有时AFNetwork上缺少参数。我的后端开发人员告诉我,您在上传图片时没有发送参数。我现在该怎么办。

1 个答案:

答案 0 :(得分:0)

试试这个

如果您的参数为nil则为paas nil而不是_params。

 NSMutableDictionary *_params=[[ NSMutableDictionary alloc]init];
[_params setObject:postsomthingtext.text forKey:@"body"];
[_params setObject:userid forKey:@"user_id"];


NSData *imageData = UIImageJPEGRepresentation(choosedimg,.6);
documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Here is the file path for the image
filepath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"imagename.png"]];
[imageData writeToFile:filepath options:NSDataWritingAtomic error:nil];

AFHTTPRequestOperationManager*operationManager = [AFHTTPRequestOperationManager manager];
operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFHTTPRequestOperation *operation = [operationManager POST:@"http://ekisansansar.com/application/webservices/Webservicefiles.php" parameters: _params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:filepath] name:@"upload_file" fileName:[filepath lastPathComponent] mimeType:@"image/png" error:&error]) {
        NSLog(@"error appending part: %@", error);
    }
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"responseObject = %@", result);




} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error = %@", error);
}];

if (!operation) {
    NSLog(@"Creation of operation failed.");
}