1

I want to upload Photo/Video from my document directory path without converting into NSData along with other parameters using AFNetworking, any suggestion if possible through any other way than suggest me.

Nikunj
  • 49
  • 7
  • Do you use multipart-formdata to upload? – Cong Tran Nov 25 '15 at 08:50
  • yes , check my code , [formData appendPartWithFileURL:[NSURL URLWithString:videoPath] name:@"video" fileName:@"video.MOV" mimeType:@"video/quicktime" error:nil];, I am trying to upload video from my docuement directory but not unfortunately not uploading the video. any other suggestion.? – Nikunj Nov 25 '15 at 11:22
  • If i use multipart-formdata than it works fine but when i tried to upload file using appendPartWithFileURL than its not working, suggestion? – Nikunj Nov 25 '15 at 11:27
  • 1
    maybe, it can help in your case? http://stackoverflow.com/questions/22180367/afnetworking-2-2-0-upload-image-on-server-issues – Cong Tran Nov 25 '15 at 11:34
  • @CongTran :- Great answer, many many thanks. superb, its working. – Nikunj Nov 25 '15 at 11:40

1 Answers1

0

Nikunj May be this will help you :)

NSDictionary *params =@{ your parmeters

                        };

//===========================================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];
}