Recently i am developing the IOS app, a feature is needed to upload image to the webservice server. I am using the AFNetworking library. The function is as follow.

-(void)SendImage:(NSData *)imgData {

    NSDictionary *parameters = @{@"ShopID": [NSString stringWithFormat:@"%d", 1],

                                 @"ImageID": @"0"

                                 };

    NSString *szAddress = utilityGetRequestAddress( EASYQ_CMD_UPLOAD_IMAGE );

    httpRequestManager.responseSerializer = [AFJSONResponseSerializer serializer];

    [httpRequestManager POST:szAddress parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFormData:imgData name:@"ShopImage"];

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

        NSLog(@"Success: %@", responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];

}

 

There is one thing need to be noticed. In the server side, i am using Slim framework, when i try to using variable $_FILES["ShopImage"] to get the image, it returns null. Finally i found i need to use $app->request->post('ShopImage') to get the image data.

posted on 2015-10-11 21:31  xsgluozi  阅读(142)  评论(0编辑  收藏  举报