使用AFNetworking上传图像数组

时间:2015-06-11 07:41:09

标签: php ios afnetworking image-uploading multipartform-data

我正在尝试使用AFNetwoking上传图像数组。 Web服务需要以下类型的参数:

action: customer create album
deviceID: jsdkgf786
AlbumName: Drive testing
UserType: Customer
UserID: 1
EventId: 2
Privacy: 1
$_FILES[‘avtar’] array of files.

我正在使用此代码上传我的文件。但不知何故,响应正常,没有任何错误,文件没有上传,因为当我拿取图像时,“null”即将到来。

代码如下。

-(void)creatNewAlbum:(NSMutableArray *)arr_images withNames:(NSMutableArray *)arr_imageNames{
    // some extra work: will move it to different method
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"ddMMMyyyy_hhmmss"];


    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:serverUrl]];
   //[NSString stringWithFormat:@"AlbumName_%@",[formatter stringFromDate:[NSDate date]]]
    NSDictionary * dicParamsToSend  = @{
                                        @"action" : customerCreateAlbum,
                                        @"UserType" : [[UserBaseClass sharedApi].user objectForKey:@"UserType"],
                                        @"UserID": [[UserBaseClass sharedApi].user objectForKey:@"UserID"],
                                        @"deviceID": [AppDelegate sharedInstance].str_deviceUID,
                                        @"Privacy": [NSString stringWithFormat:@"%ld",(long)self.int_albumType],
                                        @"EventId": @2,
                                        @"AlbumName": self.str_albumName

                                        };
    [SVProgressHUD show];
    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dicParamsToSend constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        int i = 0;
        for(UIImage *eachImage in arr_images)
        {
            NSData *imageData = UIImageJPEGRepresentation(eachImage,1);
            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"avtar[%d]",i ] fileName:[NSString stringWithFormat:@"%@",[arr_imageNames objectAtIndex:i]] mimeType:@"image/jpeg"];
            i++;
        }
        NSLog(@"the data to be sent is %@", formData);
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
        if ([[responseObject valueForKey:@"status"] isEqualToString:@"success"]) {
            UIAlertView * alert_success = [[UIAlertView alloc] initWithTitle:@"Success" message:[responseObject valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert_success show];
            [self firstLoad];
            [SVProgressHUD dismiss];
        }

        else{
            UIAlertView * alert_success = [[UIAlertView alloc] initWithTitle:@"Error!" message:[responseObject valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert_success show];
            [SVProgressHUD dismiss];
        }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {

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


    [op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        //[progressView setProgress: totalBytesWritten*1.0f / totalBytesExpectedToWrite animated: YES];
        NSLog(@"Sent %lld of %lld bytes and progress is %f", totalBytesWritten, totalBytesExpectedToWrite, totalBytesWritten*1.0f /  totalBytesExpectedToWrite);
        if(totalBytesWritten >= totalBytesExpectedToWrite)
        {
            //progressView.hidden = YES;
        }
    }];

    [op start];
}

请提供解决方案,因为它占用了大量时间。 任何帮助将不胜感激。 提前谢谢。

0 个答案:

没有答案