AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]
initWithBaseURL:[NSURL URLWithString:@"http://192.168.1.7/rakyesh/test/rest/rest/upload"]];
NSData *imageData= UIImagePNGRepresentation([UIImage imageNamed:@"appl.png"]);
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"http://192.168.1.7/rakyesh/test/rest/rest/upload"parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:@"image" fileName:@"appl.png" mimeType:@"image/png"];
}success: ^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSLog(@"success!");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
}];
Asked
Active
Viewed 1,090 times
0

halfer
- 19,824
- 17
- 99
- 186

Mohammad irani
- 23
- 1
- 5
-
this code for post an image...but i want to upload video to my own server... – Mohammad irani Mar 21 '16 at 10:48
1 Answers
2
You can do it with following code
For Image Uploading
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
Here is the reference for it AFNetworking.
For Uploading Video
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:videoData name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];

Piyush
- 1,534
- 12
- 32
-
i want upload video from photo library of simulator using AFNetworking 3.0 – Mohammad irani Mar 21 '16 at 11:20
-
thanks...but this is for an image....i want to upload video selecting from photo album of simulator and post it to my own php server... – Mohammad irani Mar 21 '16 at 11:23
-
-
-
` NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];` – Piyush Mar 21 '16 at 12:17
-
NSData *imageData= UIImagePNGRepresentation([UIImage imageNamed:@"appl.png"]); here imagedata is pass....but for video i don't know how to set a path.. – Mohammad irani Mar 21 '16 at 12:17
-
http://stackoverflow.com/questions/10480170/how-to-select-any-video-or-movie-file-from-uiimagepickercontroller – Piyush Mar 21 '16 at 12:21