dourao1896 2013-11-18 12:17
浏览 57

上传文件只传输几个字节的数据-IOS

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:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";filename=\" %@ \"

", key,videopath] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:videoData]];
        [body appendData:[[NSString stringWithFormat:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [body appendData:[[NSString stringWithFormat:@"--%@--
", 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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)