使用AFNetworking从文档目录路径上传视频/照片

时间:2015-11-25 08:36:02

标签: ios iphone afnetworking

我想从我的文档目录路径上传照片/视频,而不使用AFNetworking转换为NSData以及其他参数,如果可能的话,建议通过除建议我以外的任何其他方式。

1 个答案:

答案 0 :(得分:0)

Nikunj可能会帮助你:)。

NSDictionary * params = @ {                           你的参数

                        };

//===========================================AFNETWORKING HEADER

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFFormURLParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

//===============================SIMPLE REQUEST

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:kAddMarketProduct parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
                                {
                                 for (int i = 0; i<[arry_MarketImageArry count]; i++)
                                    {
                                        NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate];

                                        if ([[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"type"] isEqualToString:@"0"])
                                        {

                                            [formData appendPartWithFileData: [[arry_MarketImageArry objectAtIndex:i]valueForKey:@"data"] name:[NSString stringWithFormat:@"image%d",i+1] fileName:[NSString stringWithFormat:@"%lf-image%d.png",timeInterval,i] mimeType:@"image/jpeg"];


                                        }
                                        else if ([[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"type"] isEqualToString:@"1"])
                                        {

                                            NSData *data_Video=[[NSMutableData alloc]initWithContentsOfURL:[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"data"]];

                                            [formData appendPartWithFileData:data_Video  name:[NSString stringWithFormat:@"image%d",i+1] fileName:[NSString stringWithFormat:@"%lf-video%d.mp4",timeInterval,i] mimeType:@"video/mp4"];


                                            [formData appendPartWithFileData: [[arry_MarketImageArry objectAtIndex:i]valueForKey:@"thumb"] name:[NSString stringWithFormat:@"videoimage%d",i+1] fileName:[NSString stringWithFormat:@"%lf-thumb%d.png",timeInterval,i] mimeType:@"image/jpeg"];


                                        }


                                    }

                                }];



//====================================================RESPONSE


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

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

}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSError *error = nil;
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];

    [delegate.activityIndicator stopAnimating];
    [self ResponseAddMarketProduct:JSON];
}