3

Hello Friends, I am new to iPhone. I want to upload the video on server. I have tried it with several ways but only few bytes is transmitted to server. bellow is my code for iOS

I have pasted my code here. Thanks in Advance.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *urlString = [info valueForKey:UIImagePickerControllerMediaURL];
    self.videoData = [NSData dataWithContentsOfURL:urlString];
  //  NSLog(@"%@",self.videoData);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

    videopath= [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/Video.mov",documentsDirectory]];

    BOOL success = [videoData writeToFile:videopath atomically:NO];

    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
    NSLog(@"video path --> %@",videopath);


    [self uploadVideo];

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"Error");
}

-(void)uploadVideo
{
    self.responseData = [[NSMutableData alloc] init];
    NSString *urlStr;

    urlStr = [NSString stringWithFormat:@"http://iwillstudy.com/videomash/UploadToServer.php?pid=%@&uid=%@",[self.dictDetail objectForKey:@"id"],appDel.strEmailId];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"****";

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    NSMutableDictionary *sendData = [[NSMutableDictionary alloc]init];

    [sendData setValue:videoData forKey:@"uploaded_file"];


    for (id key in sendData)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";filename=\" %@ \"\r\n\r\n", key,videopath] 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"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    [request setURL:[NSURL URLWithString:urlStr]];

    HUD = [[MBProgressHUD alloc] init];
    [self.view addSubview:HUD];
    [HUD setDelegate:self];
    [HUD setLabelText:@"Loading...."];
    [HUD show:YES];

    NSLog(@"FBLogin URL:%@",urlStr);

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

}

Bellow is my php file for uploading the video

<?php
    include "dbconn.php";
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) 
{
echo "success - file uploaded";
    $sql="INSERT INTO surveyvideos (name, path,added)
VALUES
('$file_path','$file_path',Now())";

if (!mysqli_query($con,$sql))
 {
 die('Error: ' . mysqli_error($con));
 }
echo "1";
} else{
echo "0";
}
?>

Please help me guys I am stuck with this since last 3 days, can you please tell me what mistake I've been doing.

Nixit Patel
  • 4,435
  • 5
  • 30
  • 57

0 Answers0