-(IBAction)uploadToServer :(id)sender
{
NSString *str1=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"intro.mp4"];
NSLog(@"str1=%@",str1);
NSString *escapedUrlString = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"escapedUrlString=%@",escapedUrlString);
NSURL *videoURL = [NSURL URLWithString:escapedUrlString];
NSLog(@"videoURL=%@",videoURL);
NSData *newdata = [NSData dataWithContentsOfFile:escapedUrlString];
webdata=[NSData dataWithData:newdata];
NSLog(@"webData = %@",webdata);
[self post:webdata];
}
- (void)post:(NSData *)fileData
{
NSData *videoData = fileData;
NSString *urlString = @"http://rompio.com/web_service/web.php?method=upload_video&user_id=4";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:videoData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"returnString=== %@", returnString);
}
Asked
Active
Viewed 1.9k times
6

Rajan Balana
- 3,775
- 25
- 42

rahul
- 147
- 2
- 2
- 10
-
Write you code properly so It will easy to understandable to others. Any way what is problem you are getting while uploading ? – Bhumeshwer katre Nov 20 '13 at 06:32
-
http://stackoverflow.com/questions/20047607/uploading-file-only-transmits-few-byes-fo-data-ios/20069694#20069694 – Dilip Manek Nov 20 '13 at 06:32
-
In this Code.WebData is coming Null.Also Video is not successfully getting posted to the server. – rahul Nov 20 '13 at 06:33
-
Hi Mr.Bhumeshwer Katre.The problem is that video is not getting posted to the Server.. – rahul Nov 20 '13 at 06:34
-
Hello Dilip.Thanks for URL refrence.But in that code they have used AFHTTPRequestOperation .But where i can find this file for IOS7 compatible. – rahul Nov 20 '13 at 06:38
-
In that link i have given the answer which use the AFNetworking and its compatible with iOS7. Just check it out. – Dilip Manek Nov 20 '13 at 06:44
-
@user2754190 : Is `newdata` null ? – Maulik Nov 20 '13 at 06:47
-
Yes New data and WebData..Both are coming empty – rahul Nov 20 '13 at 07:02
-
1AFNetworking will help – amar Nov 20 '13 at 07:04
-
@user2754190: what is `videoURL` ? remote url or local file's url ? – Maulik Nov 20 '13 at 07:10
2 Answers
6
It's easy to do with the AFNetworking library and you can also use it to track the progress of the video upload. You can download AFNetworking library from here.
And for configuring AFnetworking please refer this Link.
And this code will used to send the video on server
NSString *videoURL = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath: videoURL]];
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.example.com"]];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/videoupload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
[formData appendPartWithFileData:videoData name:@"file" fileName:@"myVideo.mov" mimeType:@"video/quicktime"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
{
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Video Uploaded Successfully");}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"Error : %@", operation.responseString);}];
[operation start];

Kenster
- 23,465
- 21
- 80
- 106

Dilip Manek
- 9,095
- 5
- 44
- 56
-
I download the file for AF networking.But there is not such file name consist of AFHTTPClient – rahul Nov 20 '13 at 07:21
-
There is some changes in the new AFNetworking update please refer this.. http://stackoverflow.com/questions/19502673/afnetworking-afhttpclient-class – Dilip Manek Nov 20 '13 at 07:27
-
[operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) Now i am getting error in just this line. Incompatible block pointer types sending 'void (^)(NSInteger, long long, long long)' to parameter of type 'void (^)(NSUInteger, long long, long long)' – rahul Nov 20 '13 at 07:28
-
Comment those code its just for tracking the progress. And i will look into it for the error. – Dilip Manek Nov 20 '13 at 07:33
-
Or you have to download `AFHTTPRequestOperation` explicitly and add it in your project. Bcoz `AFHTTPRequestOperation` is not part of the afnetworking – Dilip Manek Nov 20 '13 at 07:35
-
Hello Dilip. I am using this web service.http://rompio.com/web_service/web.php?method=upload_video&user_id=4 And i am using the code above which you suggest me.But i am not getting success.Can you please try and upload any video.and help me – rahul Nov 20 '13 at 08:05
-
Ok Thanks …I am waiting friend..As this is very urgent to me ..I am trying for this last three days . – rahul Nov 20 '13 at 08:14
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41515/discussion-between-user2754190-and-dilip) – rahul Nov 20 '13 at 09:18
0
Store selected video URL in some variable :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
selectedVideoURL = info[UIImagePickerControllerMediaURL];
}
}
For latest version of AFNetworking
, below code is used.
NSMutableDictionary *dictParams1 = [[NSMutableDictionary alloc]init];
[dictParams1 setObject:userID forKey:"userID"];
[dictParams1 setObject:otherparam forKey: "otherparam_Key"];
NSLog(@"REQUEST URL : ------------------ %@", API_URL);
NSLog(@"PARAMETERS : ------------------ %@", dictParams1);
NSString *strFileName = [NSString stringWithFormat:@"%ld.mov", [[NSDate date] timeIntervalSince1970]];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:APIAddFeedVideo parameters:dictParams1 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:selectedVideoURL name:@"videofile" fileName:strFileName mimeType:@"video/mov" error:nil]; // video/quicktime //video/mp4
} 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(@"VIDEO UPLOAD RESPONSE : %@ %@", response, responseObject);
}
}];
[uploadTask resume];

VRAwesome
- 4,721
- 5
- 27
- 52